Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 09-23-2016, 02:53 PM
jeffreybrown jeffreybrown is offline Loop through a column Windows Vista Loop through a column Office 2007
Expert
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Hi Don,

Trust me, there's enough code I don't understand either, but getting good enough to craft something together to get me by.

This x is a variable which is initially set to -1. I basically means, from the active cell, -1.

Therefore, as we place the first Countif in column B, if you -1, the Countif is pointing to column A.

The .Value = .Value is basically taking all values in B2:B21 and making them values.

Code:
Sub ApplyCountif()
    Dim LastRow As Long
    Dim LastCol As Long
    Dim i As Long
    Dim RPosition As Long
    Dim rngData As Range

    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column

    Const x As Long = -1
    Const sFormula As String = "=IF(R[" & x & "]C[" & x & "]<>RC[" & x & "],COUNTIF(C[" & x & "],RC[" & x & "]),"""")"

    For i = 1 To LastCol Step 2

        LastRow = Cells(Rows.Count, i).End(xlUp).Row - 1
        RPosition = InStr(1, Cells(1, i), 1)

        Application.ScreenUpdating = False

        With Cells(2, i + 1).Resize(LastRow)
            .Formula = sFormula
            .Value = .Value
            If RPosition <> 0 Then
                Set rngData = Cells(2, i + 1).Resize(LastRow)
                rngData = Evaluate(rngData.Address & "*2")
                .NumberFormat = "0;;;"
            End If
        End With

    Next i

    Application.ScreenUpdating = True

End Sub
No doubt somebody else could've or would've coded this requirement different, but if it achieve the desired result, the only thing left is speed.

To get a better picture of what the macro is doing, Step through the code using F8. You might also want to read up on debugging code or setting watches.

http://www.mrexcel.com/forum/lounge-...cks-gurus.html

http://www.excelforum.com/the-water-...hers-wont.html

http://www.mrexcel.com/forum/general...lications.html
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through a column Timeline - loop through all dates between first and last given and add date to column if not found carlos_cs Excel Programming 1 05-03-2016 09:25 AM
Loop through a column documents saved with double column revert to single column when re-opened danw Word 6 04-08-2015 06:24 AM
Loop through a column Embedded Excel in Powerpoint - custom animation, display column by column andytheanimal PowerPoint 2 01-20-2015 06:30 AM
How can I temporarily break a 3 column format in order to type a single column paragraph William P Word 1 01-04-2015 06:40 PM
Want a quotient using a constant to show up in column c each time a number is added to column b fibbermcghee Excel 2 12-09-2014 05:48 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:22 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft