Sheet declaration goes wrong - excel

I have been using vba for a while and i noticed an annoying bug.
When i use more than 1 sheets in a macro if i set the worksheets everything works just fine until i get an error. After that the worksheet names are messed up and usually uses both declaration for as one sheet.
Dim loc_path, sp_path As String
Dim loc_wb As Workbook
Dim sp_wb As Workbook
Dim loc_ws As Worksheet
Dim sp_ws As Worksheet
loc_path = "path1"
sp_path = "path2"
Set sp_wb = Workbooks.Open(sp_path, ReadOnly:=False)
Set sp_ws = sp_wb.Sheets(1)
Set loc_wb = Workbooks.Open(loc_path, ReadOnly:=False)
Set loc_ws = loc_wb.Sheets(1)
After that i just call another macro and pass the worksheet or sheets. In an example i pass only the sp_ws to the macro and display the first cells value in a msgbox. Before error it works fine, but after an error if i run it again it will display the loc_ws sheets first cells value.
Is there anything wrong with my code or it is just a bug in VBA?

Related

How to pass main workbook to another macro function?

I have to macro function, where the second one calls for the first one in the loop.
I want to remember the main workbook and worksheet where the macro function was executed.
I wrote the following code%
Dim main_book As Workbook
Dim main_sheet As Worksheet
Set main_book = ActiveWorkbook
Set main_sheet = ActiveSheet
Dim table1 As Range
MsgBox main_book.Name
Set table1 = main_book.main_sheet.Range("A1", "J2")ˇ
But the last row returns an error, however if I hardcode the name of the workbook and worksheet everything works just fine. Can't find a solution and reason for this error

Issue with worksheet object and LoadPicture in VBA

I have been working on an excel macro and have come across an issue that doesn't make sense to me.
I have on two of my sheets, (Sheets("Printing Sheet") and Sheets("Printing Sheet B"), an Xactive image object called slipLogo. I created a subProcedure that should change the image loaded into slipLogo and the color of other cells on the sheet depending on an options selected within a userform. The userfrom also decides which of the two slipLogos and therefore sheets are being changed. But I was having problems with the image changing portion, so I pulled out the troublesome code to do some troubleshooting on it and focused on just one of the two sheets.
The issue is that I get the error "method failing" which points to the slipLogo part of ws.slipLogo.Picture=LoadPicture(C:Users\Logo.jpg). However when I use instead the following line Sheets("Printing Sheet").slipLogo.Picture=LoadPicture("C:\Users\Logo.jpg") everything works.
This indicates to me that for some reason ws does not equal Sheets("Printing Sheet").
Sub Help5()
Dim ws As Worksheet
Set ws = Sheets("Printing Sheet")
Dim imagePath As String
imagePath = "C:\Users\Logo.jpg"
ws.slipLogo.Picture = LoadPicture(imagePath)
'Sheets("Printing Sheet").slipLogo.Picture = LoadPicture(imagePath)
With Sheets("Printing Sheet").slipLogo 'the ws also does not work at this location
.Height = 35
End With
End Sub

Use of an if statement with open workbook variable

I am trying to simplify some code, and wondered whether I would be able to use an If statement to set a different open workbook variable, depending on a few conditions of a defined range.
basically something like
dim ws as worksheet: set ws = Thisworkbook.worksheet("worksheet name")
dim r as range: set range = ws.range("A cell which has an if statement in it to display a number based on another cells contents")
dim wb as workbook
this is where I want to vary the actual workbook set as wb
if r.value = 1 then
set wb = Open a workbook
elseif r.value = 2 then
set wb = Open another workbook
Ideally I would then do the same for ws2 which would be the specific worksheet variable for the specified workbook above
Not sure if this method will work and wondered if I was on the right track. I have code which opens a workbook, finds values in various rows, inserts rows, copies cell contents., between two ws variables, etc. And it works, but I need ot add more workbook options and Id rather not have multiple macros with the only difference being the variable for the second workbook to open and work with.
Its OK - tried the above and it seems to work fine - just lacked a bit of confidence I suppose.

How to hide columns in a VBA named range?

I'm pretty new to VBA and I'm trying to hide some columns in workbooks.
I have to do it in VBA because the workbook is an Analysis for Office workbook, so I have to hide some columns that the final users don't have to see.
My problem is that I can't point to a specific letter of the columns, because, since the workbook is modifiable like a pivot with other dimensions in respect of the default ones with which it opens, if I point to a specific column in the code, it won't be the same when an user add new dimensions. So I have to find the column with its name ad hide it.
So for now I tried this:
Sub Hide_Columns()
'Dim Sourcecell As Range
Dim wb As Workbook
Dim ws As Worksheet
'Set Sourcecell = Nothing
'Set Sourcecell = ThisSheet.Range("SapCrosstab1").Find("Colonna di calcolo", LookIn:=xlFormulas)
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Demand Planning")
wb.Sheets("Demand Planning").Range("Colonna di calcolo").EntireColumn.Hidden = True
End Sub
The problem here is that I'm having the
"Run-time error 1004: Application-defined or object-defined error"
and I don't know how to fix it.
I commented that 3 lines because I'm not so sure they could help.
Your current attempt fails because Range("Colonna di calcolo") will not search for a column with that header. Instead, it want to access a Named Range with that name, and if you don't define this, it will throw exactly this error 1004.
I think your attempt with SourceCell was not bad - issue a Find command and hide the EntireColumn of the found cell. Your mistake was that there is no object ThisSheet. There are objects ThisWorkbook which refers to the entire workbook where the code is stored, or ActiveWorkbook which is the Workbook that is currently shown (not necessarily the workbook where the macro is stored).
You can solve your issue either by defining a Named Range for the column - in that case your code should work.
Or change the code to something like
Dim ws as Worksheet
Set ws = ThisWorkbook.Sheets("Demand Planning")
Dim Sourcecell as Range
Set Sourcecell = ws.Range("1:1").Find("Colonna di calcolo", LookIn:=xlFormulas)
If Not Sourcecell Is Nothing Then
Sourcecell.EntireColumn.Hidden = True
End If

vba excel increment number value in cell on another workbook?

I am trying to use vba to update a number value in a cell on another excel workbook by 1.
so if I have the value 466 in workbook 2 then when I run my code form workbook 1 this will update 466 to 467, and each time the code is run it gets incremented by 1.
the workbook will normally be closed but I want it to be able to work whether the workbook is open or closed, if that matters.
I am trying to use the following code but its not updating anything and I am not getting any errors.
Please can someone show me what I am doing wrong. Thanks
Dim ws1112 As Worksheet, ws2221 As Worksheet
Set ws1112 = Sheets("Statistics")
Set ws2221 = Workbooks("\\UKSH000-File06\Purchasing\New_Supplier_Set_Ups_&_Audits\Workbook 2.xls").Sheets("Dashboard")
ws2221.Range("C7").Value = ws2221.Range("C7").Value + 1
Now I'm going to go out on a limb and say that your error is "Error '9', subscript out of range" but that this error is somehow being suppressed or silently handled by the rest of your code (for example an On Error GoTo statement).
This is occurring because you can't open a workbook simply by including the full filepath in your Workbooks("<name>") statement. You'll need to use Workbooks.Open("<FilePath>") for that.
For example:
Dim wb As Workbook
Dim ws1112 As Worksheet
Dim ws2221 As Worksheet
Set ws1112 = Sheets("Statistics")
Set wb = Workbooks.Open("\\UKSH000-File06\Purchasing\New_Supplier_Set_Ups_&_Audits\Workbook 2.xls")
Set ws2221 = wb.Sheets("Dashboard")
ws2221.Range("C7").Value = ws2221.Range("C7").Value + 1
'Optional if you want to close the workbook afterwards
wb.Close SaveChanges:=True

Resources