VBA Code That Consider Variable Change
May 25, 2011
Trying to create a VBA code that will enter a formula such as: SUM(Q2:Qt) in a range of cells for instance A2:A10, and if there is information in B11, B12, B13 etc., it will consider that variable change and enter the SUM formula in cells A11, A12, A13.
I read that Dim LR As Long is the solution but not sure how to apply.
View 9 Replies
ADVERTISEMENT
Sep 8, 2006
I need my program to:
- find the cell containing the string "Datum/Tid"
- record the column and the row of the found cell in two variables lCol and lRow
Here is my
Sub test()
Dim rFoundCell As Range
Dim lRow As Long
Dim lCol As Long
'Find method of VBA
Set rFoundCell = Range("A1")
Set rFoundCell = Worksheets("Sheet1").Range("A1:Z50").Find(What:="Datum/Tid", After:=rFoundCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
'for anyof the two lines down I get the message "object variable OR block variable not set"
lRow = rFoundCell.Row
lCol = rFoundCell.Column
End Sub
View 5 Replies
View Related
Apr 3, 2014
I have problem to change text code into numerical code using macro. i have data contain text code and i would like to convert it into numeric . each text code has dedicated numerical code for example I have 4 fluids with text code text code:
FW = Fresh water
SW = Saline eater
CW = connate water
MW = Meteoric water
numeric code as follow
FW=1
SW=2
CW=3
MW=4
I would like to convert the text code into numeric code, it is easy if using excell , but it is routine job for me , i need to create macro to be more simple .
View 1 Replies
View Related
May 12, 2009
I am trying to change the variable value in my following code through array.
What I want is that both the statements
Debug.Print testarray(0) & "........" & testarray(1) & "......." & testarray(2)
Debug.Print custname & "........" & custaccount & "......." & worthcredit & vbCrLf & vbCrLf
should deliver me the same values i.e changedname 123456 and true
for testarray(0),testarray(1),testarray(2) i am getting the values but I am not able to change the variable values for custname ,custaccount and worthcredit, although I am accessing the same elements.
Here is full ....
View 12 Replies
View Related
Jul 30, 2014
I have a list of Variables Dimentioned a Range. They are sequential; ie. A1, A2, A3, etc. How do I write the code to switch between these? Obviously, I am looking to correctly reference ("A" & aNo).
Code:
For Test = 1 To aNo
With ("A" & aNo)
.Select
[Code]......
View 9 Replies
View Related
Jun 22, 2009
I am trying to write a formula in code that uses a changing variable, "X".
here is what i have thus far:
.Value = (("G" & X) + 1) - (("G" & X) + ("B" & X))
I am trying to say: ((GX)+1)-((GX+BX))
X is a variable that changes in a loop. it will be the row number. so for example:
(G18+1)-(G18+B18)....I AM APPEARNTLY NOT WRITING THE SYNTAX CORRECTLY.
View 9 Replies
View Related
Mar 27, 2012
I have recorded the code below that does what I need, problem is I will use the code on files with various amounts of rows. What can I change in it so it will work on all different size files.
Code:
Sub Macro3()
'
' Macro3 Macro
[Code]....
View 6 Replies
View Related
Mar 25, 2014
I have a text variable MtgDate containing "25/03/2014"
I need to produce another text variable (to build into a file name) MtgDate1 with the text "2014-03-25"
I was going to use the Substitute function to replace the "/" with a "-" then Mid to juggle the dd-mm-yyyy to yyyy-mm-dd but at the moment I can't even find the right syntax for the Substitute.
Got as far as:
[Code] .....
but this just sets MtgDate1 as "=Substitute(MtgDate, " / ", " - ") ie reduces the double quotes to single ones.
View 2 Replies
View Related
Feb 19, 2009
How do I change the selection of a range with a variable, and not a hardcoded number in XL2003? I have to update a set of spreadsheets every month, and it's a hassle to have to constantly open my pivot table worksheet, copy, open the summary worksheet, paste ... etc. etc. etc. The code below is my attempt at creating a ComboBox with "January, February, March, etc." and every time I select a particular month it will automatically copy data from my pivot table worksheet into my new summary worksheet in the correct column. The range of data from my pivot table worksheet will never change, so I have no problem hardcoding that in, but based on which month is selected will alter which column the data goes in in the new worksheet.
I want to be able to write code for one month (say January) and then when I want to use a different month (say February) I can just change the column number and call up the originial January code.
Private Sub ComboBox1_Change()
Dim ColNum ' This is the variable I want to change based on which month is called
If ComboBox1.Value = "January" Then
ColNum = 1 ' Column number for Column A- Where my January column is
Elseif ComboBox1.Value = "February" Then
ColNum = 2 ' Column number for Column B- Where my February column is
' etc. etc. for each month
End If
Call January
End Sub
Private Sub January()
' Just a quick msgbox to make sure the previous macro is calling this one
MsgBox "Is this macro running?", vbQuestion + vbYesNo, "Check"...........................
View 9 Replies
View Related
Oct 19, 2009
I need the variable here to be 7 numbers long, if not I need to add zero's at the beginning. However the problem I am having here is when then number of zero's is greater than one.
Basically in the example below the variable 'Zeros' = 2 so I need the variable 'Variable' to give answer "00", how do I do this!?
If Len(ActiveCell.Value) 7 Then
Zeros = 7 - Len(ActiveCell.Value)
Variable = CStr("0") * Zeros
End If
View 10 Replies
View Related
Oct 22, 2013
I have goal table on table A and i want it data fill to tabel B with constant sum. maybe like sudoku in sum right and sum down.
View 1 Replies
View Related
Sep 24, 2009
it's possible to change the caption (title) of a user form to a variable or cell value? Or does it always have to be hard coded?
View 2 Replies
View Related
Apr 25, 2009
This is the code on the Worsheet Change event.
Private Sub Worksheet_Change(ByVal Target As Range)
'Define the RMAs range and count the RMA ships. Do the rest of the math based on this calculation.
Dim intRMACol As Integer, EndRow As Integer, intRMAShips As Integer, LastCol As Integer
Dim rngRMAs As Range, rngHome As Range, sht As Worksheet
Set sht = ActiveWorkbook.ActiveSheet
Set rngHome = Cells(1, 1)
rngHome.Activate
EndRow = Range("A" & Rows.Count).End(xlUp).Row
intRMACol = Cells.Find(What:="RMA#", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column
Set rngRMAs = rngHome.Offset(1, intRMACol - 1).Resize(EndRow, 1)
rngRMAs.Name = "RMAs"
rngHome.Offset(1, LastCol + 1).Formula = "=COUNTA(RMAs)"
intRMAShips = rngHome.Offset(1, LastCol + 1).Value
rngHome.Offset(1, LastCol).Formula = "=SUM((" & EndRow & " - 1) - " & intRMAShips & ")"
rngHome.Offset(1, LastCol + 2).FormulaR1C1 = "=SUM(RC[-2] : RC[-1])"
End Sub
I keep getting stuck when trying to set the intRMACol variable. I have used the code all over the place, but only in Modules, never on a worksheet event. EndRow, the variable right above, sets correctly so I don't get it.
Also, do I have to define the Target variable? Am I going about this the right way? I basically want three formulas to calculate when any change is made on the worksheet.
View 9 Replies
View Related
Feb 25, 2013
How do you drag down a column to auto complete the values but do it so it only changes one variable.For example, I am dragging down =IF(ROW()<=$B$5,MOD(E2*B1,B2),"") down my column.
However, I only want the "E2" value to change, and I want B1 and B2 to stay that way. Whenever I drag down, it keeps changing all three values. Is there a way to make it just so that the E2 value is the only one that changes?
View 4 Replies
View Related
Jan 12, 2010
I'm trying to make a spreadsheet more secure to prevent unintentional changes.
What i'm looking to do is to lock all cells so no changescan be made unless the user clicks column A.
Column A should remain unlocked and when the user clicks it i would like the entire row unlocked for editing (but only after column A is clicked)
I will run a macro to lock the entire sheet again once the sheet closes.
View 2 Replies
View Related
Jul 9, 2012
0
284.6554
419.3615
565.5539
[Code].....
The row on the top currently matches the column on the left side. This was done by using simple commands like "=A4", "=A5", "=A6" in the top row. Now I am trying to expand this table to a variable amount of columns and rows (there will always be 3 more rows than columns like there currently is above), so I want to write some code in VBA that will put these equations in the cells for me.
Here is the tricky part though. The numbers in the left column are actually calculated using the solver, and the numbers in the middle cells reference both the left column and top row. Because of this, I actually need to put an equation in the top row (like the previously mentioned "=A4") so that it always has the newest result from the solver.
The left column will always be in the same row, so only the number itself needs to change, but I'm not sure how to do this.
View 3 Replies
View Related
Feb 4, 2007
I have been using this code to auto print a series of workbooks whose file date contains the day i want. Each if statement looks to a different folder location to find the spreadsheet with the desired date and auto prints that sheet before moving on to the next if and spreadsheet, and then finally closing. I use an input box to get the date/day variable.
The spreadsheets i auto print are in folders labeled for different months of the year, i.e. 0107, 0207 etc. I want to be able to enter the MONTH i want the spath to look for, in other words, i want to be able to input first the month, which tells the rest of the routine what folder to look for the next input, which is the date of the spreadsheet. I am flummoxed by the variable itself.
The code is below.
Public Sub Auto_Open()
Dim sCurFile As String
Dim sPath As String
fpath = InputBox("shift and day (BXX) of the month to print?", "print")
'Get the path
sPath = "k:2007207"
If sPath <> "" Then
On Error Resume Next
Application. ScreenUpdating = False
If Right(sPath, 1) <> "" Then
sPath = sPath & ""
End If
View 4 Replies
View Related
Feb 20, 2008
I'm looking for a way to identify the caller procedure and to act according to that.
I tried the CALLER command but it seems not to be the appropriate one. The sample code is as follows:
Sub AAA()
MsgBox "This MsgBox is displayed from within Proc. AAA"
BBB
CCC
MsgBox "This should NOT to be shown if Proc CCC was called from here"
End Sub ............
View 8 Replies
View Related
Feb 15, 2012
I am acquiring multiple spreadsheets that do not always match row number, due to additional information on some sheets. By this I mean that the information may be in row 31 on on sheet and row 39 on another, the column location is the same each time. I have been trying Vlookups, indexing and matching plus combination formulas - with no luck. How to get the information I need with a moving cell reference?
I can provide a small copy of a workbook, if needed.
View 1 Replies
View Related
Mar 6, 2009
I have a spreadsheet that has several buttons on that run code using the cells around the code for parts of the data. eg
ColumnA1 ColumnB1 ColumnC1 Button1
ColumnA2 ColumnB2 ColumnC2 Button2
I want the buttons to run a seperate piece of code to start, but then all buttons run a similar piece of code at the end. I am hoping to be able to pass a variable from the first piece of code each button runs into the section of code that all share (which I have done as a seperate macro)
View 9 Replies
View Related
Jun 8, 2009
I have a spreadsheet with about 30 charts on it that I would like to attach a macro to which opens the chart in a form. I've figured out how to do this last part, but am stuck on how to pass a variable to the form code which tells excel which chart to copy. Essentially what I'm doing creating a macro for each chart which would run when that chart is clicked on. This macro is identical for each chart except for the name of the chart being passed.
Sub Chart1_click()
ShowChart "Chart 1"
End Sub
Sub ShowChart(c As String)
frmChart.Show
End Sub
In the UserForm_Initialize code located in the userform module, I'd like to call the code which saves and loads the image of the clicked on chart. But here's where I'm stuck. How do I pass c to the code in the form module...e.g, how to pass c to UserForm_Intialize and to ChartZoom? Here's what I have so far...
Private Sub UserForm_Initialize(c As String)
ChartZoom c
End Sub
Private Sub ChartZoom(c As String)
frmChart.Show
Dim Cht As Chart
Set Cht = ActiveSheet.ChartObjects(c).chart
Dim CName As String
CName = ThisWorkbook.Path & "cht.gif"
Cht.Export Filename:=CName, FilterName:="GIF"
imgCht.PictureSizeMode = fmPictureSizeModeZoom
imgCht.Picture = LoadPicture(CName)
End Sub
View 9 Replies
View Related
May 19, 2007
I am trying to figure out how to use a variable as a row number for use in a range name and/or a pivot table range. Right now I have a range of R571C17, but the row number will change with each use of the pivot table formation macro.
View 3 Replies
View Related
Aug 7, 2007
Just a niggling problem, I've got lstRow as a Long and it contains the value of the last row offset by (1, 0). The problem is i'm trying to add it into a range
Range("C500:K500").Select
Selection.autofill Destination:=Range(C" & lstRow &":K" & lstRow &")Type:=xlFillDefault
Like that.. Except i've tried a million different combinations of " and & in different places to try and get it to compile. It refuses too. It always gives the error "Expected list seperator or )" I know I could just do it individually from C to K but I'd like to learn how to do it this way as well.
View 6 Replies
View Related
Jun 3, 2014
I have declared a 'long' variable. however; how do I change the variable data type to string if user input is not a number? how do I recognize the user input's datatype?
View 7 Replies
View Related
Feb 2, 2014
I would like a function which works like the NPV function except that the interest rate should be variable and a range rather than a single number.
Is it possible to see the code for the excel NPV function so that I can modify it?
Or alternatively has seen such a function?
View 8 Replies
View Related
Dec 31, 2008
I have piece of code that I'm trying to make dynamic. I want to store parts of an IF statement in a variable and use them when I need them. Here's a little sample of what I'm trying to do
View 9 Replies
View Related
May 29, 2014
I need a code which will open up a previous days file. I have the code to pick the correct file but I am stuck because the folders are multiple levels down to year then month.
Eg. Folder Name > YYYY > Month
So i need to have a code which can look in to the relevant folder based on the current date and then be able to open the file i need.
I.e. today is 29May so i need the code to open the 2014>May folder.
For 1st day of month I would need it to open last months folder I.e. 01June Open May folder
View 2 Replies
View Related
Feb 12, 2010
I suppose this is kind of SQL as much as anything so apologies if this isn't really the right place for it.
I had an idea of how to use a concatenated string in Excel to utilise the SQL IN statement for listing parameters that vary from day to day.
Annoyingly my code does not work:
If my range of codes is for example
XS0330509760
XS0242821717
XS0241172948.................
View 9 Replies
View Related
May 5, 2009
I am trying to write VBA code that will print a print range that is presented in cell F3 on a "Reports" worksheet. The content of F3 will change depending on how many reports the user selects to print. For example, he could select one, two, three reports etc - up to twelve. The cell ranges of each report are named (e.g. Report1, Report2 etc) so that if the user selects to print Reports 1 and 2, the contents of cell F3 are "Report1,Report2". If I replace WhatToPrint with "Report1,Report2" the print macro works.
Sub Macro2()
Dim WhatToPrint As String
WhatToPrint = Sheets("Reports").Cells(3, 6).Value
'sets the variable to equal the contents of cell D3 which contains the formula
'summarising the print ranges I want to print
Sheets("Reports").Cells(3, 6).Select
ActiveCell.FormulaR1C1 = WhatToPrint
' pastes the variable in cell F3 - just to check that it looks like I want it to
Sheets("Reports").PageSetup.PrintArea = WhatToPrint
'uses the variable to set print area - this is where it fails!
'if you replace the variable with the contents of cell F3 the macro will work
ActiveWindow.SelectedSheets.PrintPreview
End Sub
View 3 Replies
View Related
Jul 18, 2006
I have a set of worksheets (Sheet31 through Sheet49 by codename) produced by a machine in the lab. I want to create a summary sheet that references cells in those worksheets.
Rather than go through the annoyance of doing this manually 40+ times I'm working on creating a macro to populate the summary sheet for me.
This is what I have so far
For J = 31 To 49
ResultSht = SheetJ.Name
Sheet1.Activate
ActiveSheet.Range("A4").End(xlDown).Offset(1, 0).Select
It hangs up on the "SheetJ.Name" however. My objective is to be using the object SheetJ, where the object SheetJ is the codename of the results sheet I'm trying to reference (for example, in the first pass through the loop it would be Sheet31). Could someone point out my mistake in this object reference?
View 9 Replies
View Related