VBA for ThisWorkbook.Cells works with numbers only

StuartWhi

Board Regular
Joined
Sep 1, 2011
Messages
75
Hi Experts,

I have converted some different code found on the site to do what I want but have two small issues I hope someone can assist me with.

Basically what I want to do is find a blank set of cells within a column between to values then copy the top value to all cells (then continue down the column).

The two issues,

How to change this to the ActiveSheet not a fixed sheet…
Code:
Set rng = ThisWorkbook.Worksheets(2).UsedRange

How to change this to allow for a combination of Numbers and Letters (at the moment it only works for numbers (courses an “run-time error ‘13’: Type mismatch) if letters are in the cell).
Code:
Factor = rng.Cells(StartRow, ColNo).Text

Full code below.

Code:
Option ExplicitSub CopyDown()


Dim rng As Range, RowNo As Long, Recs As Long, ColNo As Variant
Dim StartRow As Long, EndRow As Long, Factor As Double


MsgBox ("Please back-up before selecting ""Ok"" impossible to recover after you continue.")


Set rng = ThisWorkbook.Worksheets(2).UsedRange
ColNo = Application.InputBox("Enter required column letter.", "Column Input", Type:=2)  ' Not accepting letters
              
StartRow = FindNoEmpty(rng, 1, ColNo)
EndRow = FindNoEmpty(rng, StartRow + 1, ColNo)


Do While (StartRow <= rng.Rows.Count) And (EndRow <> 0)
    Recs = EndRow - StartRow
    Factor = rng.Cells(StartRow, ColNo).Text


        For RowNo = StartRow + 1 To EndRow - 1
            rng.Cells(RowNo, ColNo).Value = Factor
        Next RowNo
        
        StartRow = EndRow
        EndRow = FindNoEmpty(rng, StartRow + 1, ColNo)
Loop
MsgBox "Complete, hope it worked.  Re-run for different columns", vbInformation


End Sub
Function FindNoEmpty(rng As Range, ByVal StartRowNo As Long, ByVal Col As Variant) As Long
Dim RowNo As Long
    
If StartRowNo <= 0 Then
    Exit Function
End If
    
For RowNo = StartRowNo To rng.Rows.Count
    If Not IsEmpty(rng.Cells(RowNo, Col)) Then
        FindNoEmpty = RowNo
        Exit Function
    End If
Next RowNo
        
End Function

Thanks in advance.

Stuart.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You can try

Code:
Set Rng = ActiveSheet.UsedRange


Also,Dim Factor as Variant instead of Double
 
Upvote 0
Hi Peter,

The Variant killed me (had tried the ActiveSheet.UsedRange...), as always great answers by great people.

Catch you next time.

Stuart.
 
Upvote 0

Forum statistics

Threads
1,216,489
Messages
6,130,959
Members
449,608
Latest member
jacobmudombe

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top