I would like to turn Excel formulas with string functions into VBA equivalents.
There are several lines of formulas. I'm going to give two examples.
There is a copy a txt report in column A. Columns B thru O sort through the rows of A, picking out pieces using some of the following formulas:
=IF(MID(A6,60,1)="=",MID(A6,52,15),IF(MID(A6,57,1)="=",MID(A6,53,8),C1))
and
=IF(A2="","-",IF(LEFT(A2,1)="1","pg","-"))
Here's a snapshot of a test report.
I'd like to loop through the formulas and send to Sheet2 starting with A2 (due to headers) not go through the steps of copying then pasting to A1, copying columns B thru O, and then sorting out the "-".
I tried recording a macro and imputing the formulas, but I couldn't make it happen.
Sub convert()
Dim ws As Worksheet, thisRng As Range
Set ws = Sheet1
Set thisRng = Sheet1.Range("B1")' Select the cell to place data into
' Break up the formula logically first and it becomes easier to convert
' =IF (MID(A6,60,1)="=", MID(A6,52,15), else if IF(MID(A6,57,1)="=", MID(A6,53,8), else C1))
If Mid(ws.Range("A6").Value, 60, 1) = "=" Then
thisRng = Mid(ws.Range("A6").Value, 52, 15)
Else
If (Mid(ws.Range("A6").Value, 57, 1)) = "=" Then
thisRng = Mid(ws.Range("A6").Value, 53, 8)
Else
thisRng.Value = ws.Range("C1").Value
End If
End If
End Sub
Related
Is there a way to sum all the values of the same cell of multiple sheets (tab) which the name of each tab is date, and show the summed value in the MasterSheet?
For example:
Currently, I use this formula =SUM(INDIRECT("'*-"&MONTH(C2)&"-2020'!$E$3")) in the cell under the "Oct-2020" cell, as shown in the figure below, and I will do the same for Nov-20 and Dec-20.
As you can see, I am getting this "#REF!" error. Currently, I have two tabs that I am trying to get the "$E$3" cell values from, "13-10-2020" and "14-10-2020". However, if I only have one sheet (let's say "14-10-2020" only), I am able to get the value.
Does anyone know what is going on with my formula? Why it works when there is only one sheet (tab) to read from, but does not work when there are multiple sheets (tabs), even I used "*" to include all dates.
Please advice. Thank you in advanced
I think this should solve your problem:
Option Explicit
Sub sub_sum_up_months()
Dim wks As Worksheet
Dim nr_month As Integer, nr_year As Integer
Dim str_month As String, str_year As String
Dim c As Integer
Dim my_Sum As Double
For c = 3 To 5 ' Iterating over the columns "C" to "E"
my_Sum = 0
nr_month = month(Worksheets("MasterSheet").Cells(2, c).Value)
If nr_month < 10 Then
str_month = "0" & nr_month
Else
str_month = CStr(nr_month)
End If
nr_year = Year(Worksheets("MasterSheet").Cells(2, c).Value)
str_year = CStr(nr_year)
For Each wks In ThisWorkbook.Worksheets
If Right(wks.Name, 4) = str_year And Left(Right(wks.Name, 7), 2) = str_month Then
my_Sum = my_Sum + wks.Cells(3, 5) 'cells(3,5) is "E3"
End If
Next wks
Worksheets("MasterSheet").Cells(3, c).Value = my_Sum
Next c
End Sub
You can use the sum function.
Follow this step by step and it will work.
In your MasterSheet C3 type =Sum(
Click on sheet 13-10-2020 and cell E3.
Now press and hold shift.
Click on the last sheet that is October and press Enter.
Now you should get the sum of all E3 in October sheets.
As far as I know there is no formula to do this dynamic but I'm quite sure it can be done in VBA if the sheet names follow a pattern.
i have this worksheet called Test. In that worksheet i want to export to pdf then print cells G1 to cells H17. Next i want to print G1 to cells I17 skipping column H meaning without column H. In another word column G is kind of like the header and the columns that follow the result. So print Header column+ results column and so on until column CM. Below each printing page i want to also export to pdf column C18, column D18 and Column H18 if cells I18 to cells I27=500. I started the code but couldn't go on to finish it since i am not really versed in vba
Sub SetPrintArea()
Dim ws As Worksheet
Dim x As Long, result As Boolean
result = True
For x = 18 To 128
If Worksheets("AR-MD").Range("H" & x).Value <> ActiveSheet.Cells(8, 19).Value Then
result = False
End If
If Not result Then Exit For
Next x
Set ws = ThisWorkbook.Sheets("AR-MD")
ws.PageSetup.PrintArea = Union(ws.Range("g1:g17"), ws.Range("i1:i17"),ws.Range("Result").Address
End Sub
I want to be able to print the result condition but i dont know how to include it in the statement
Use the Union method:
ws.PageSetup.PrintArea = Union(ws.Range("g1:g17"), ws.Range("i1:i17")).Address
In the attached table I would like to link cell contents via VBA.
Column A contains contents that should be linked to the cells in column B until a new content is added to A.
The example in the attached table is shortened. The tables are much longer.
I have made a 'before' and an 'after' sheet.
The code I have is linked from A to B until a new value comes into A. But the VBA always takes the newest value in column B and not all the previous ones.
How can I adjust the code so that all values from B are linked to A until a new value comes into column A?
Sub Linking_columns_until_new_values()
Dim arr
Dim z As Long
Dim txt As String
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
With ActiveSheet.UsedRange.Columns(1).Resize(, 2)
.Columns(1).SpecialCells(xlCellTypeBlanks).Font.Bold = False
arr = .Value
For z = 1 To UBound(arr)
If arr(z, 1) <> "" Then
txt = arr(z, 1)
ElseIf arr(z, 2) = "" Then
arr(z, 1) = ""
Else
arr(z, 1) = txt & " " & arr(z, 2)
End If
Next
.Value = arr
End With
Next
End Sub
Here is the link to the sheet:
https://www.evernote.com/l/AGApoCGk-OJGKaHxwB2F-VCjO9uWJN299TM/
There is no need to use VBA for this:
In B1, because it's first entry, I copied manually.
My formula in B2 is =IF(B2="";"";C1&B2) and drag down
My formula in C1 is =B1 and drag down
Later on you could copy/paste formats, paste values, and then delete columns A and B. That way you would get what you got in After Sheet
Probably you can use the macro recorder to get the code and adapt it to your needs if you need VBA solution.
Hope this helps
I have 2 excel files:
barkod.xlsx
campaign.xlsx
I need to make a vlookup command in barkod.xlsx. I want to lookup The column E in barkod.xlsx through the campaign.xlsx - columns A:B.
I also tried to double drag the formula till the last value of E at the end. The result will be written in column F in barkod.xlsx.
I tried with this codes :
Private Sub CommandButton5_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim x As Workbook
Dim y As Workbook
'## Open all workbooks first:
Set x = Workbooks.Open("C:\Users\mammadov.ali\Desktop\macros\barkod.xlsx")
Set y = Workbooks.Open("C:\Users\mammadov.ali\Desktop\macros\csv.csv")
Set q = Workbooks.Open("C:\Users\mammadov.ali\Desktop\macros\campaign.xlsx")
'## Clear the workbook first:
y.Sheets("csv").Range("A:M").Clear
'## Insert the column in the barkod file:
x.Sheets("barkod").Range("F1").EntireColumn.Insert
'## Insert the column header in the barkod file:
x.Sheets("barkod").Range("E1").Offset(0, 1).Value = "Discounts"
With x.Sheets("barkod").Range("F2")
.FormulaR1C1 = "=VLOOKUP(RC[-1], [campaign.xlsx]Sheet1!RC[-5]:RC[-4], 2, 0)"
.AutoFill Destination:=x.Sheets("barkod").Range("F3")
End With
I am struggling with this almost 2 days. Any help would be highly appreciated.
What I get is it does not show me the correct result and it only does the vlookup for : A2:B2 only. Also it occurs an error during the run.
you have to:
remove "xlsx" and "R"s from [campaign.xlsx]Sheet1!RC[-5]:RC[-4], 2, 0)"
have destination range of AutoFill method encompass source range
as follows:
With x.Sheets("barkod").Range("F2")
.FormulaR1C1 = "=VLOOKUP(RC[-1], [campaign]Sheet1!C[-5]:C[-4], 2, 0)"
.AutoFill Destination:=.Resize(2) ' this will autofill F2 and F3
End With
if you want to AutoFill as many cells in column F as not empty values in column E, then you would use:
.AutoFill Destination:=.Resize(WorksheetFunction.CountA(.Offset(, -1).EntireColumn))
Lets say we have 5000 rows with random values (blanks, numbers, characters). I need to show type of all the cells in these 5000 rows in a single cell using a formula. Is it actually possible? I've tried to CONCATENATE(CELL("type";array)) and ctrl+shift+enter but it didn't work (it returns the type of the first cell in the array).
If you want to know, this is for finding a cell with text rather than values or blanks in a very big file. Maybe you have a better solution.
Thanks in advance.
UPD: thanks for macros but I can't use them in this workbook, I need a formula-solution.
UPD: I've got how to do it with conditional formatting => new rule => use a formula to determine... => use ISTEXT('first cell of the range') formula
But still, is it possible to create the formula?
The best way to go about this is to use MACROs
Here is my sample code:
Sub Button2_Click()
numRows = 10 ' Number fo rows to loop through, in your case 5000
'loop through each cell located in column 1
'Check its type
'Concatenate each one in 1 cell on the 8th column
For i = 1 To numRows
Sheet1.Cells(1, 8).Value = Sheet1.Cells(1, 8).Value & TypeName(Sheet1.Cells(i, 1).Value) & ","
Next i
End Sub
You can adapt this small user defined function to your needs.
Say we are looking at cells in the first row, from A1 through D1 and we want to concatenate their types into a single cell. Enter the following UDF() in a standard module:
Public Function KonKaType(rIN As Range) As String
Dim r As Range
For Each r In rIN
s = "cell(""type""," & r.Address(0, 0) & ")"
KonKaType = KonKaType & Evaluate(s)
Next r
End Function
and then in the worksheet cell E1 enter:
=KonKaType(A1:D1)