I am very much a beginner when it comes to VBA programming.
I have a Macro that hides or shows columns based on the value in one cell:
Sub HideColumnsMacro()
Range("b8:o8").EntireColumn.Hidden = False
v1 = Range("b2").Value + 1
If v1 < 12 Then
With Range("b8")
Range(.Offset(0,v1), .Offset(0, 12)).EntireColumn.Hidden = True
End With
End If
End Sub
I want to be able to get this same functionality when I change a cell on a different sheet. Is there a way I can tell this Macro to act on this sheet, when it is run from a different one?
In your macro, specify the exact sheet:
Sheets("Sheet1").Range("b8:o8").EntireColumn.Hidden = False
Qualify your Ranges with the name of the worksheet:
Sheet1.Range("b8:o8").EntireColumn.Hidden = False
Related
I'm copying values as part of one sub process and pasting value through an update button on userform.
To copy values:
Private Sub Month1_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.AskToUpdateLinks = False
Dim wkb As Workbook
Dim wks As Worksheet
Set wkb = Workbooks.Open("Place on drive")
Set wks = wkb.Sheets("Training1")
wks.Range("Start:Finish").Copy
wkb.Close
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
Application.ScreenUpdating = True
End Sub
To paste values in current sheet:
Private Sub UpdateActuals_Click()
For i = 1 To 12
If Me.Controls("Month" & i).Value = True Then
ThisWorkbook.Sheets("2017 Actuals").Range(i+1, 5).PasteSpecial xlPasteValues
End If
Next i
End Sub
If I replace "i+1, 5" with "B5", it errors with
"PasteSpecial method of Range class failed".
I feel as if values copied in one sub process are not brought to second one, would that be correct?
Also, how do I reduce processing time given that I have 12 months (12 files) in various places that I can't change the location for...
Range usually likes a starting cell and an ending cell. I suggest since you are looking at just one cell that you change .Range to .Cells. If you really want to use a range with RC format, .Range(Cells(row1, col1), Cells(row2, col2)), if you want just one cell then you can make the two parts the same. I have run into problems before using Range and only one cell definition before, either make it .Cells for your target or fill out Range the way I have explained.. Cheers.
Dim 2017actWS AS Worksheet
Set 2017actWS = ThisWorkbook.Worksheets("2017 Actuals")
1)
2017actWS.Cells(i+1, 5).PasteSpecial xlPasteValues
-or-
2)
2017actWS.Range(2017actWS.Cells(i+1, 5), 2017actWS.Cells(i+1,5)).PasteSpecial xlPasteValues
When using Ranges excel will often throw errors if they are not the same size in a copy and paste, you can eliminate that by using a single cell as the starting target of your paste with .Cells
Also I don't see you call your function. You will want your paste close to your copy or you might find things get strange (suggestion: just after your copy).
Edited to be sure there is not worksheeet ambiguity. Thank you Scott C.
Cheers, WWC
first of all I'm not a VBA programmer but typically an end-user who uses snippets of code that I gather in forums like this in his spreadsheets, trying to understand what the code does. Code for which I thank you all!
My objective:
I have a spreadsheet that is basically an input-form for users.
Based on their input and selections via dropdown my intention is to guide them through the form by hiding & unhiding rows with input fields, presenting the users with the relevant questions.
On each row I have created an IF-formula that creates a 1 or 0 based on previous provided input
1 -> unhide the row , 0 -> hide the row.
So I'm looking for a macro that runs with every sheet calculation and hides or unhides the next rows as needed.
These formulas are in range I3:I70 on top of that I created a summary field in I2 =sum(I3:I70) so i thought I can either check changes in the range I3:I70 or changes on cell I2 to trigger the macro. [Neither solution fixed my problem]
I've tried several code examples discribed on the forums and I've tested the macros that checks for change in the range or the cell individually.
As long as I call a test macro with a MsgBox it works fine.
Also the macro that hides or unhides runs fine when I call it manually.
My problem:
When I let the 'auto'-macro call the 'hide'-macro, Excel simply crashes; no warnings, nothing --> just crash.
My code:
Private Sub Worksheet_Calculate()
Dim Xrg As Range
Set Xrg = Range("H3:H70")
If Not Intersect(Xrg, Range("H3:H70")) Is Nothing Then
Macro1
End If
End Sub
Sub Sample()
MsgBox "Yes"
End Sub
Sub Macro1()
Dim cell As Range
For Each cell In Range("H3:H70")
If Not IsEmpty(cell) Then
If cell.Value = 0 Then
cell.EntireRow.Hidden = True
End If
If cell.Value = 1 Then
cell.EntireRow.Hidden = False
End If
End If
Next
End Sub
Thanks for any suggestions and tips in advance.
JeWe
Never give up searching :-) I gave it a last search and found some code on the microsfof dot com site that seems to work.
Don't ask me the details but this seems to do what i'm looking for
Private Sub Worksheet_Calculate()
Dim LastRow As Long, c As Range
Application.EnableEvents = False
On Error Resume Next
For Each c In Range("H3:H70")
If c.Value = 0 Then
c.EntireRow.Hidden = True
ElseIf c.Value = 1 Then
c.EntireRow.Hidden = False
End If
Next
On Error GoTo 0
Application.EnableEvents = True
End Sub
It's late on my end of the world, going to sleep. Will update tomorrow.
Txs JeWe
I'm trying to write a macro that combine the defined sheets from different workbooks and saves them to a separate workbook. Each workbook contains a sheet with the same name MODEL. This sheet has a combobox, this combobox is associated with the worksheet name like in code below:
Sub ComboBox1_Change()
Application.ScreenUpdating = False
Select Case Sheets("MODEL").Range("code_plant")
Case 1
Sheets("MODEL").Range("price_zero").Copy _
Destination:=Sheets("MODEL").Range("price_on_view")
End Select
Application.ScreenUpdating = True
End Sub
The problem arises when all the worksheets are combined together in one workbook and renamed. I need to fix my combobox macro that should after renaming refer to the current (renamed) sheet name. I was trying to use the following function but can't figure out how it may be include in my code in right way.
Function MySheet()
Application.Volatile
MySheet = Application.Caller.Worksheet.CodeName
End Function
Thanks!
Your ComboBox1_Change event is declared in the worksheet module, so you should be using the Me keyword in your code, to refer to the sheet, instead of finding the sheet by name with Sheets("MODEL").
Change your event handlers, in each source sheet to look like this:
Sub ComboBox1_Change()
Application.ScreenUpdating = False
Select Case Me.Range("code_plant")
Case 1
Me.Range("price_zero").Copy _
Destination:=Me.Range("price_on_view")
End Select
Application.ScreenUpdating = True
End Sub
Since your combobox is in the same sheet, you do not need to specify the sheetname or use anything else to qualify it. The range will point to Activesheet (Where ComboBox1 resides) which is the Sheets("MODEL") or whatever it's new name is.
Sub ComboBox1_Change()
Application.ScreenUpdating = False
Select Case Range("code_plant")
Case 1: Range("price_zero").Copy Range("price_on_view")
End Select
Application.ScreenUpdating = True
End Sub
I am currently using Excel 2011 on Mac, and I am trying to pull data using a QueryTable, but I am forced to have to import the entire table. I am currently pasting the entire table onto a hidden sheet, then using a formula to pull data from the specific cell. To show you what I mean, Here is a sample:
Sub Yield()
Dim URL As String
Dim qt as QueryTable
Dim hs As Worksheet
Set hs = Worksheets("Hidden Sheet")
Set ws = Worksheets ("Managed Equity Portfolios")
URL = "http://www.nasdaq.com/symbol/spy"
Sheets("Hidden Sheet").Visible = True
Set qt = hs.QueryTables.Add( _
Connections:= "URL;" & URL, _
Destination:= hs.Range("A1"))
qt.RefreshStyle = xlOverwriteCells = True
qt.BackgroundQuery = True
qt.SaveData = True
qt.Refresh Background Query:= False
'I am using the same parameters here as my other code, honestly not
'sure why a lot of it is on here, but I kept it because it works.
Worksheets("Hidden Sheet").Visible = False
End Sub
Currently, the information I need is being pasted into the Hidden Sheet in cell B42. I made it so the cell that I want the information ='Hidden Sheet!'B42 but this doesn't seem very efficient. Is there any way I could have just the cell B42 be put into Excel?
I'll be on here to clear up any questions, thank you so much!
I am creating an macro-enabled Excel as a tool for generating 'create table' sql script. The sheet is created where one needs to enter the column names, data type etc., and on a button click the script will be generated. This sheet is called 'Script Generator'. Now I need an 'Index' sheet which will have table names and a button. When I click the button I need to open 'script generator' sheets for each table name and these sheets should be renamed to the table name.
The index sheet code goes like this:
Sub Add_sheets()
On Error Resume Next
Dim count As Integer
Dim r As Range
Dim sh As Worksheet
For Each r In Range("A3:A103")
If Not r = "" Then
count = 0
For Each sh In ActiveWorkbook.Sheets
If sh.Name = r.Value Then
count = count + 1
End If
Next sh
If count = 0 Then
With ActiveWorkbook.Sheets
.Add(after:=Worksheets(Worksheets.count), Type:="C:\Macro\Script_Template.xltm").Name = r.Value
End With
ActiveSheet.Hyperlinks.Add Anchor:=r, Address:="", _
SubAddress:=Sheets(r.Value).Name & "!A1"
End If
End If
Next r
End Sub
Now, the problem is I am adding the script generator saved as 'Script_Template.xltm' externally. I need only one Excel which will do this all. Means, the Index file should internally open/add the new sheets of the format 'script generator' so that it forms one complete tool. Maybe by hiding this sheet and calling its instances through macros and renaming those sheets. How to do it through VBA? Could someone help me with this?
Using True and False for setting the Visible property of a worksheet is not good practice. You should use the constants already provided - xlSheetHidden, xlSheetVeryHidden and xlSheetVisible.
xlSheetVisible will make your sheet visible and xlSheetHidden will hide your sheet. Setting it to xlSheetVeryHidden will ensure that the only way you can make the sheet visible is through VBA and not through the Excel menubar Format -> Sheet -> Unhide.
Usage:
Sheets("Script_Template").Visible = xlSheetVisible
You can create a "Script_Template" sheet and hide it and then use this code create a copy
Sheets("Script_Template").Visible = True
Sheets("Script_Template").Copy After:=Sheets(ThisWorkbook.Sheets.count)
ActiveSheet.Name = r.Value
Sheets("Script_Template").Visible = False