saving vba variable

B-Man

Board Regular
Joined
Dec 29, 2012
Messages
183
Office Version
  1. 2019
Platform
  1. Windows
I have a vba macro open a workbook and pull some data to display in a user form and it works fine.

Now I want to close the workbook it opens before it displays the data but when I close the workbook after getting the values but before displaying it they don't display. I don't know if I need to dim as variant and set the values but when I try that I get an object error. so im now out of my depth.

ie. my vba contains a heap of these from textbox 1 to 10, but when I close the workbook the user form just has blank values. (note bImported is just stolen from another piece of code I had that works how I want and just haven't renamed it to suit...)
VBA Code:
Giftcards.TextBox1 = Format(wb.Sheets("Giftcards").Range("T2").Value, "$   #,###")

    ' Close the workbook if it was opened by the code
            If bImported Then
            wb.Close SaveChanges:=False
            End If
    
Application.ScreenUpdating = True
Giftcards.Show
 
that worked perfectly Dave
I had to change this
VBA Code:
 Const wbPath As String = "[B]C:\Users\B-Man\Documents\MyFolder\[/B]"

to this as I was getting an error "const expression required" when trying to use Environ("userprofile") due to different users needing to run it.
VBA Code:
dim wbPath   As String
wbPath = Environ("userprofile") & "\Google Drive\Giftcards\

Right. That's what I would normally need. I have your code but it is useless to me without the UserForm, or the other file you are getting data from. Since I am unable to figure out what's wrong from analyzing the code statically I'll step aside and hopefully someone else will see an issue.
Yes I know how hard it is without all the information. I couldn't see where the code was going wrong myself but a fresh code fixed it so must have been something there.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
that worked perfectly Dave
I had to change this
VBA Code:
 Const wbPath As String = "[B]C:\Users\B-Man\Documents\MyFolder\[/B]"

to this as I was getting an error "const expression required" when trying to use Environ("userprofile") due to different users needing to run it.
yes that change would cause an error but glad you managed to resolve yourself & appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,132
Members
449,097
Latest member
mlckr

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