VBA worksheet referencing

Fudging

New Member
Joined
Aug 26, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello,

I found this VBA code that converts a range into a table. It works perfectly fine, my only issue with it is " Set OpenWb = Workbooks.Open("C:\Users\user\Desktop\data.xlsb")". Does this mean that if someone else tries to run this code on their computer it won't work as I set it under my username? If so, is there a way change this to simply refer to a specific worksheet in the current excel file?

VBA Code:
Sub converttbl()

    Dim OpenWb As Workbook
    Set OpenWb = Workbooks.Open("C:\Users\user\Desktop\data.xlsb")
    Dim wsData As Worksheet
    Set wsData = OpenWb.Worksheets("Data")
OpenWb.Worksheets("Data").Range("A1").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection.CurrentRegion, , xlYes).Name = _
    "MyTable"
MsgBox "Execution completed"


End Sub

I also found this VBA code below as well but the example he used was an excel file with only one worksheet, so it didn't work for me, is there a way to specify exactly what worksheet I want it to run in? I don't know if the code below is better than the above...

VBA Code:
Dim src As Range
Dim ws As Worksheet
Set src = Range("B5").CurrentRegion
Set ws = ActiveSheet
ws.ListObjects.Add( SourceType:=xlSrcRange, Source:=src, _
xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Sales_Table"

Thanks for the help :)
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
What is the sheet name & range you want to convert?
 
Upvote 0
What is the sheet name & range you want to convert?
I ended up solving it myself, thanks for the help though.

This is what I did:
VBA Code:
Sub Convert2Table()

    Dim wsData As Worksheet
    Set wsData = Worksheets("Consolidated Table")
Worksheets("Consolidated Table").Range("A2").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection.CurrentRegion, , xlYes).Name = _
    "ConsTbl"

End Sub
 
Upvote 0
Solution
Glad you sorted it & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,223,391
Messages
6,171,820
Members
452,426
Latest member
cmachael

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