"Autofill" code works in vba when executed by commandbutton, but not in a subroutine module

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
335
Office Version
  1. 2010
Platform
  1. Windows
The code shown below gives me the correct result that I am looking for when executing out of a commandbutton located on the same worksheet:

VBA Code:
    ' take the names previously copied-and-pasted and reverse the first name wiht the last:
    Range("B1").Select
    ActiveCell.FormulaR1C1 = _
        "=MID(RC[-1]&"" ""&RC[-1],FIND("" "",RC[-1]),LEN(RC[-1]))"
    'takes the copied strings in B1, trim, and then put the result in C1
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
    'takes the previous 2 formulas and copies-and-pastes them down the the last row that contains a name:
    Range("B1:C1").Select
    Selection.AutoFill Destination:=Range(Cells(1, 2), Cells(wCol, 3)), Type:=xlFillDefault
    'take the contents from the first row (in column B) and down to the bottom row (column C):
    Range(Cells(1, 2), Cells(wCol, 3)).Select
    ActiveWindow.SmallScroll Down:=-174
    '******
    Range(Cells(1, 3), Cells(wCol, 3)).Select 'copies the selection:
    Selection.Copy
    'pastes the copied data from the above code and pastes it starting at "D1" as VALUES:
    ActiveWindow.SmallScroll Down:=-189
    Range("D1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

it returns this:
Capture3.PNG


However, when I use the same code in a module and execute it that way, I get this as the result (where it copies the first name in A1 and pastes that same name down to the last row instead of capturing each unique name in each cell down to the last row...

Capture2.PNG

Here is the module code for that (I have tried different iterations of the code below, but whatever I try, I just get the same result with only capturing the same first name that appears in A1:

VBA Code:
    ' take the names previously copied-and-pasted and reverse the first name wiht the last:
    ActiveWorkbook.Worksheets("SwitchNames").Range(Cells(1, 2), Cells(wCol, 2)).Select
    ActiveCell.FormulaR1C1 = _
        "=MID(RC[-1]&"" ""&RC[-1],FIND("" "",RC[-1]),LEN(RC[-1]))"
    'takes the copied strings in B1, trim, and then put the result in C1
    ActiveWorkbook.Worksheets("SwitchNames").Range(Cells(1, 3), Cells(wCol, 3)).Select
    ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
    'takes the previous 2 formulas and copies-and-pastes them down the the last row that contains a name:
    ActiveWorkbook.Worksheets("SwitchNames").Range("B1:C1").Select
    Selection.AutoFill Destination:=Range(Cells(1, 2), Cells(wCol, 3)), Type:=xlFillDefault
    'take the contents from the first row (in column B) and down to the bottom row (column C):
    ActiveWorkbook.Worksheets("SwitchNames").Range(Cells(1, 2), Cells(wCol, 3)).Select
    ActiveWindow.SmallScroll Down:=-174
    '******
    ActiveWorkbook.Worksheets("SwitchNames").Range(Cells(1, 3), Cells(wCol, 3)).Select 'copies the selection:
    Selection.Copy
    'pastes the copied data from the above code and pastes it starting at "D1" as VALUES:
    ActiveWindow.SmallScroll Down:=-189
    ActiveWorkbook.Worksheets("SwitchNames").Range("D1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Oh,and I did have to change up the code slightly by identifying the range with the full-name/location:

so instead of:

VBA Code:
Range("B1").Select

as it appears (and works) in the commandbutton code, I had to change that to:

VBA Code:
ActiveWorkbook.Worksheets("SwitchNames").Range(Cells(1, 2), Cells(wCol, 2)).Select

to keep from getting an error.

Thanks for any suggestions
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,746
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Did you turn calculation off somewhere?
 
Solution

Forum statistics

Threads
1,147,453
Messages
5,741,214
Members
423,649
Latest member
steel1968

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
Top