First Post. I have a workbook that is a list of customer names, each name is a link to their individual workbook that is on a sharepoint site. The second column is a "Last contact" column which has a formula
=MAX('https://example.com/folder/Folder/Customer Library/Area/T/[Customername.xlsm]Notes'!$B$2:$B$120)
I am able to make this formula manually by opening the linked workbook, selecting the cell on my customer list, enter "=max(" then highlight the range i want on the linked workbook. Works Fine.
Enter VBA because we want to simplify the procedure for adding customers to the list. I have a userform where you type in the customer name in a textbox1, and the address of the sharepoint wb in textbox 2. The submit button has the following code which inserts the customer name with a link to the sharepoint book in target cell of column A. This works fine. I also am trying to insert the MAX formula in the adjacent cell of column B. I can get the formula to the correct place. However, I am running into issues on the web reference, as it is textbox2's value. When I use textbox2.value it gives errors. Sorry for the book, here is the code without the web book reference:
Private Sub CommandButton1_Click()
Sheets("Dashboard").Select
NextFree = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
ActiveCell.Hyperlinks.Add Anchor:=ActiveCell.Offset(0, 0), Address:=TextBox2.Value, TextToDisplay:=TextBox1.Value
Range("B" & NextFree).Formula = "=MAX(Notes'!$B$2:$B$120)"
UserForm1.Hide
Unload Me
End Sub
Between =Max( & Notes'... I know there needs to be textbox2 value, but I don't understand the syntax I suppose. The "Notes" is the sheet where the range is on the sharepoint wb. The range is the same for all additions to the customer list. Thank you for your help.
Update: I am now trying this code but get error Type Mismatch.
Private Sub CommandButton1_Click()
Sheets("Dashboard").Select
Dim SPbook As Workbook
Dim SPsheet As Worksheet
Dim SPrange As Range
Set SPbook = Workbooks.Open(TextBox2.Value)
Set SPsheet = Sheets("Notes")
Set SPrange = SPsheet.Range("B1:B120")
NextFree = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
ActiveCell.Hyperlinks.Add Anchor:=ActiveCell.Offset(0, 0), Address:=TextBox2.Value, TextToDisplay:=TextBox1.Value
Range("B" & NextFree).Formula = "=MAX([" & SPrange & "])"
UserForm1.Hide
Unload Me
End Sub
You should reference textbox2.Value as follows:
Range("B" & NextFree).Formula = "=MAX(" & textbox2.Value $ "Notes'!$B$2:$B$120)"
Formula field takes a string so you just have to build the string concatenating your values.
If textbox2.Value is giving you an error this would not resolve this. Please include more details of this error including complete error message and what values are being passed to subroutine from the form. Regards,
Related
I have written a vba code to find match in dynamic column "F" with cell value i cell "i1". And when match found in the column " F" it will clear the content of the particular row.
The VBA is working fine in the excel marco where I have written the VBA code but to my surprise when I copy the same VBA code to a different excel macro having the same content in worksheet and run the VBA code it does not clear all the match content row i.e., it clear some of the match row leaving some of match row uncleared. Where I am doing the mistake?
Sub test()
Dim i as long
For i = 100 To 1 step -1
If Range("F" & i).Value = Range("i1").Value Then Rows(i).EntireRow.ClearContent
Next i
End Sub
IMHO you just have to fix your typo and copy the code into a module (not the worksheet or workbook module) and it will work on the active sheet.
Sub test()
Dim i As Long
For i = 100 To 1 Step -1
If Range("F" & i).Value = Range("i1").Value Then
Rows(i).EntireRow.ClearContents
End If
Next i
End Sub
Rows(i) and Range("F" & i) is defined implicitly which might refer to another sheet or even another workbook.
below is a sample code with use of sheet reference.
Dim i as long
Dim ws as Worksheet
Set ws = ThisWorkBook.Sheets("YourSheetName")
For i = 100 To 1 step -1
If ws.Range("F" & i).Value = ws.Range("i1").Value Then ws.Rows(i).EntireRow.ClearContents
Next i
I am new to excel and using macros, how can i achieve below inquiry
How to add data to another column by pressing a button in excel?
When user press "SAVE", data from column B2 will be added to column of column F.
I tried to do below code but i doesn't work
Sub ButtonSave_Click()
Range("B2").Value = Range("F" & Row).Insert
End Sub
How to deal with this?
thanks in advance!
Use the following and replace Activesheet with the actual sheet name e.g. Worksheets("Sheet1")
Code:
Option Explicit
Public Sub ButtonSave_Click()
With ActiveSheet '<== replace with actual sheet reference
.Range("F" & .Cells(.Rows.Count, "F").End(xlUp).Row + 1) = .Range("B2")
End With
End Sub
Row=Range("F1").End(xlDown).Offset(1,0).Row
Range("F" & Row).Value=Range("B2").Value
I have filtered my cumulative sales from SalesMasterSheet to different sheets each named after each particular customer but I got stuck at trying to add excel formulas because my VBA code always clears content of the range of cells on sheetActive.
I have tried the special cell method so as to clear only constants but it doesn't work.
Any help would be appreciated.
below is my code:
Private Sub Worksheet_activate()
Dim i, LastRow
LastRow = Sheets("MasterSheet").Range("A" & Rows.Count).End(xlUp).Row
Sheets("ACCIMA").Range("A1:L500").ClearContents
For i = 2 To LastRow
If Sheets("MasterSheet").Cells(i, "C").Value = "ACCIMA" Then
Sheets("MasterSheet").Cells(i, "C").EntireRow.Copy _
Destination:=Sheets("ACCIMA").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
So far what it does is copy entries with "ACCIMA" on column C from Mastersheet to sheet("ACCIMA"), but i would like to put a formula in sheet("ACCIMA") but because Sheets("ACCIMA").Range("A1:L500").ClearContents
all formulas clear once i make the sheet active.
Given the low amount of informations you give I sort of understood your problem this way:
If your code always clears content of the range of cells on the active sheet you should try to add the worksheet you are working on in your code, e.g.
cells(1,1).value = "test"
would turn into
worksheets("customer1").cells(1,1).value = "test"
After new informations:
Copy the content of your range "A1:K500", ignore the column "L" which has your formular and then add your formulars in column "L".
worksheets("customer1").cells(1,1).formula = "..."
Keep in mind, that your syntax for the formula changes in comparison with the synatx on the actual excel sheet.
e.g.:
=IF(A1="empty";"empty";"not empty")
Will turn into:
worksheets("customer1").cells(1,1).formula = "=IF(A1=""empty"",""empty"",""not empty"")"
I have an Excel 2010 workbook containing 2 sheets ("Contents" and "Folders").
The purpose of this workbook is to track different pieces of work by supplier or reference number, with a front-end (the Contents page) that is simple to use, consisting only of buttons and a search box (Which isn't actually a separate box, but simply the contents of cell J8 of the Contents sheet (hereafter referred to as J8) as typed by the user.
The buttons will filter by supplier type (and work perfectly fine) but it's the user selection that I'm having trouble with.
My code for this macro is:
Sub Find_Click()
Dim userSelect As String
userSelect = "*" & Range("J8") & "*"
Sheets("Folders").Select
ActiveSheet.Range("$B$1:$B$5000").AutoFilter Field:=2, Criteria:=userSelect, Operator:=x1And
End Sub
When the 'Find' button is pressed, this should read J8, then select the Folders sheet and filter the result to show every entry in column B that contains the text in J8.
This was working fine. However, now when I try to use this macro I get a 1004 run-time error with the 'Application-defined or object-defined error' message.
Can anyone please help?
EDIT:
The Contains buttons that have macros assigned that follow this format:
Sub Button1_Click()
Sheets("Folders").Select
ActiveSheet.Range("$A$1:$A$5000").AutoFilter Field:=1, Criteria1:= _
"Criteria"
Set r = Range(Range("A3"), Range("A3").End(xlDown))
j = WorksheetFunction.CountA(r.Cells.SpecialCells(xlCellTypeVisible))
'MsgBox j
If j = 0 Then
MsgBox "There is currently no work relating to Criteria"
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("A3").Select
Sheets("Contents").Select
End If
End Sub
There is also a resest button that clears a filter and returns to the Contents sheet:
Sub Reset_Click()
ActiveSheet.ShowAllData
Sheets("Contents").Select
End Sub
Generally, you'll need to activate a cell inside the range in which you are going to use the AutoFilter.
Further more, when you are trying to use AutoFilter with wildcards (* or ?) or math test, you'll need to add an = at the start of your criteria string, so
userSelect = "=*" & Range("J8") & "*"
Then, it is not Criteria, but Criteria1 and Criteria2 if you use a second one! So you don't need an Operator in this case.
And finally with ActiveSheet.Range("$B$1:$B$5000").AutoFilter Field:=2, you are asking the code to filter on the second column of a range where there is only one column!
So if you want to filter on col B, just change Field:=2 to Field:=1
Here is the working code :
Sub Find_Click()
Dim userSelect As String
Dim wS as Worksheet
userSelect = "=*" & Range("J8") & "*"
Set wS = Sheets("Folders")
wS.Activate
wS.Range("B1").Activate
If Not wS.AutoFilterMode Then wS.AutoFilterMode = True
wS.Range("$B$1:$B$5000").AutoFilter Field:=1, Criteria1:=userSelect
End Sub
And you also had a typo in xlAnd, it was x1And ;)
For anyone who is interested, the problem ended up being in the line:
ActiveSheet.Range("$B$1:$B$5000").AutoFilter Field:=2, Criteria1:=userSelect
As the code was filtering only column B, the Field value needed to be set to '1' instead of my original '2'
Thanks to #R3uK for his invaluable help!
My prob is this:
I want to be able to use a macro to copy & calculate formula down a range of cells if radio button is on.
But I don't know how to set the variable inside the formula. The macro below should copy the formula to ranges shown (I12:I252, K12:K252, M12:M252).
The formula itself includes a subtraction of two cells in the range of C12:C252 & B12:B252. I cannot seem to reference those cells. I thinks that's the problem...
Anyway, it doesn't work. Any help would be greatly appreciated.
Thanks!
Dim shp1 As Shape
Dim shp2 As Shape
Dim i As Long
On Error Resume Next
Set shp1 = Worksheets("Worksheet").Shapes("Button 1")
Set shp2 = Worksheets("Worksheet").Shapes("Button 2")
If shp1.ControlFormat.Value = xlOn Then
MsgBox "Auto Calculating"
For i = 12 To 252
Range("I" & i).Formula = "=IFERROR(((C & i)-(B & i))*I6/(E7-E6);"")"
Range("K" & i).Formula = "=IFERROR(((C & i)-(B & i))*J6/(E7-E6);"")"
Range("M" & i).Formula = "=IFERROR(((C & i)-(B & i))*K6/(E7-E6);"")"
Next i
Else
If shp2.ControlFormat.Value = xlOn Then
MsgBox "Manually insert calculation"
End If
End If
Few improvements:
Replace the ; in your formulas with ,. ; is your local
setting, but .Formula uses the English setting!
If you want to refer to each column, you need place the i outside the quoatation marks, i.e. instead of =IFERROR(((C & i)... write =IFERROR(((C" & i & ")...
No need to loop each cell and set the formula. If you use $ in your formula properly, you can replace all with one formula: =IFERROR(($C12-$B12)*I$6/($E$7-$E$6),"")
Better use .FormulaR1C1 - this way, your formula will also work, when you would applied it to some other range. To easily convert a formula, type it normally into a cell and the run ? Selection.FormulaR1C1in the VBA Immediate Window. The above formula translates to =IFERROR((RC3-RC2)*R6C/(R7C5-R6C5),"")
Don't hard code cell references (in your case I12:K252). Better assign this range to a named range and use this as a reference. This way, your code will also work if you later add/remove rows or columns.
Don't use On Error Resume Next! This is a invitation to oversee an error that should be fixed
Optional: Alternatively to accessing the controls directly in VBA, you can also assign each one to a cell, name this cell as in step 4 and refer to in by this name. Makes your code more flexible/less complex!
So all in all, I end up with:
Public Sub YourSub()
If Range("SwitchOne") Then
Range("YourRange").FormulaR1C1 = _
"=IFERROR((RC3-RC2)*R6C/(R7C5-R6C5),"""")"
Else
If Range("SwitchTwo") Then
MsgBox "Manually insert calculation"
End If
End If
End Sub