Code Pasting code results in wrong tab

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,332
Office Version
  1. 365
Platform
  1. Windows
This button is one Sheet1 (Home). But when I run it using the button, its pasting the tab names down another tab.

What do I need to change to get it to paste the list of tabs in sheet1 / Home tab?

Code:
Private Sub CommandButton1_Click()

'Clear any Filters in A5
ClearFilterHomeA5

'Clear existing Data first
   Sheet1.Range("A6:A7000").Select
   Selection.Clear
   Range("A6").Select

'Past all Tab Names starting in A6
Dim i As Integer
For i = 1 To ThisWorkbook.Sheets.Count
Sheets(1).Cells(i + 5, 1) = Sheets(i).Name
Next i

'Sort the Tab Names
    Sheet1.Range("A6:A665").Select
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Add Key:=Range("A6:A9"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Home").Sort
        .SetRange Range("A5:A665")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
       Range("A6").Select
      
'Refresh the Power Query for Quote Database
'   ActiveWorkbook.RefreshAll

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try
VBA Code:
Sheet1.Cells(i + 5, 1) = Sheets(i).Name
 
Upvote 0
Thank you that worked.

one quick question:
this is putting the formula in starting in B5 not B6 - why

I am assuming this has to do with the above code somehow

Code:
Sub AddHyperLinkX()
'
 Dim LstRw As Long
'
    Sheet1.Select
    
    LstRw = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
'
  Sheet1.Range("B6:B" & LstRw).FormulaR1C1 = "=HYPERLINK(""#""&RC[-1]&""!E2"",RC[-1])"



End Sub
 
Upvote 0
Probably because LstRw is 5
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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