How to change values in excel acording to different sheet - vba

I have an excelsheet which contains two sheets named respectively 'Sertifika' and 'Egitim Katılım'. in Sertifika i need a button which upgrades value of cells B6, M2 and M3 to next cells from Egitim Katılım.
Example:
B6 ='Eğitim katılım'!C149
and i want to change value of B6 to,
='Eğitim katılım'!C150
how can it be possible?

Assuming you already know how to use a button, include the below code inside the command button,
Sub mac()
Dim str As String, a As String
str = Range("B6").Formula
a = Right(str, (Len(str) - InStr(str, "C")))
a = a + 1
Range("B6").Formula = "='E?itim kat?l?m'!C" & a
End Sub
The code extracts the formula in cell B6, unstrings the row number from the formula, adds +1 to it and updated the formula. I would like you to manipulate this for the other 2 cells.

Related

select entire value of a cell using VBA

I have a column full of data with multiple lines inside a single cell like below:
I am looping through all the cells in the column and I have to select values inside every cell and search it in another sheet.I know i can split the cell value using the Split() function in vba.
But where I am struck is I am unable to select the whole value of the cell and parse it as input to the split () function. Here is my sample code where i am struck:
For Each C in Range ("A1:A" & ltrow)
If C.Value <> "" Then
SrcStrng = C.Value
TextArray () = Split(SrcStrng)
.....
........
.....
The problem I face here is in SrcStrng only the first value inside the cell for example in the first cell only t#234 is getting stored, so in split string only that is passed and it is not split properly, so I am unable to search t*567. It happens for every cell.
And also in the third cell, i want to parse only the value t#345 inside the loop for searching, neglecting L1:, I am struck with that too.
Could someone help me with this please.
Here is an example to show how this can be done. Use chr(10) as delimiter
Sub t()
Dim str As String
str = Range("A1").Value
Dim parts As Variant
parts = Split(str, Chr(10))
For i = LBound(parts) To UBound(parts)
MsgBox (parts(i))
Next i
End Sub

I have 3 excel formulas that I would like to fill ranges in a spreadsheet with, how can I make sure the cells change with the rows?

=IF(AND(G2<>100,TODAY()>=H2, TODAY()<=I2), E2, " ")
=IF(N2=" ", " ",NETWORKDAYS(H2,TODAY()))
=IF(OR(O2 = " ", O2 <= 0), " ", (O2/N2)*100)
These are the three formulas, I want to make sure that as they are inserted into the worksheet the cell references will still change to match the rows they are on, as they would in a normal spreadsheet. Any advice would be much appreciated! (To clarify, I need to fill the ranges using VBA as the code I'm using clears the worksheet every time it is run.)
you could use FormulaR1C1 property of range object, which uses the "R1C1" notation for range addresses
for instance inserting your first formula in "A1" would be:
Range("A1").FormulaR1C1 = "=IF(AND(RC7<>100,TODAY()>=RC8, TODAY()<=RC9), RC5, "" "")"
where the pure R would assume the current cell row index, while C7 stands for a fixed (not varying with host cell position) 7th column index reference, and so on
If i have interpreted your question correctly, you need something like the below:
Option Explicit
Sub InsertFormula()
Dim i As Long
Dim n As Long
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To n
Cells(i, 1).Formula = "=IF(AND(G2<>100,TODAY()>=H2, TODAY()<=I2), E2, "" "")"
Next i
End Sub
replace the 1 in n=... with whichever column has the most rows of data
replace for i = 1 to whichever row it must begin form
You will notice i have added extra quotations to the end of the formula, this is needed as quotes in a formula in VBA must be enclosed... in more quotes lol
Apply this concept for the other formulas :)
Instead of absolute references like G2 you can use something along
.FormulaR1C1 = "=SUM(RC[-2]:R[5]C[-2])"
where R and C reference the offset from the current cell (positive: right or down, negative: up or left).
Use it in a way similar to this:
Dim c
For Each c In Selection
c.FormulaR1C1 = "=SUM(RC[-2]:R[5]C[-2])"
Next c
Relative References are adjusted when you set the formula to range of cells:
[A1:B2].Formula = "=C$1" ' now the formula in B2 will become "=D$1"
You can also set multiple formulas at once:
Range("K2:M9").Formula = Array("=IF(AND(G2<>100,TODAY()>=H2, TODAY()<=I2), E2, "" "")", _
"=IF(N2="" "", "" "",NETWORKDAYS(H2,TODAY()))", _
"=IF(OR(O2 = "" "", O2 <= 0), "" "", (O2/N2)*100)" )
or if each row has different formula:
[A1:Z3] = [{"=1";"=2";"=3"}]

VBA/Excel Find a string in a cell to determine the next cell's value

I need to find a string in a cell, and if that string is found, the cell next to it has to put that string in the next cell. In layman's terms:
IF "Medina" is found in a C3
put "ME" in cell D3;
ELSEIF "Brunswick" is found in C3
put "BR" in D3;
ELSE
put "OTH"in D3;
And this has to go through out the sheet, i.e., C4 & D4, C5 & D5, etc.
Thanks in advance
Sounds like you're looking for the InStr function (see this link: https://msdn.microsoft.com/en-us/library/8460tsh1(v=vs.90).aspx). Basically we want to check if there is a non-zero answer for this, which indicates the string can be found.
So you could do something like below. Note I just put in 40 as your last row arbitrarily, but you can change this as you need.
Dim indStartRow As Integer
Dim indEndRow As Integer
Dim i As Integer
indStartRow = 3
indEndRow = 40
For i = indStartRow To indEndRow
If InStr(Sheets("Sheetname").Range("C" & i).Value, "Medina") > 0 Then
Sheets("Sheetname").Range("D" & i).value = "ME"
ElseIf InStr(Sheets("Sheetname").Range("C" & i).Value, "Brunswick") > 0 Then
Sheets("Sheetname").Range("D" & i).value = "BR"
Else
Sheets("Sheetname").Range("D" & i).value = "OTH"
End If
Next i
I don't think you need VBA for this, Excel Formulas are sufficient. If you want your Selection criteria to be easily extendable, use this:
=IFERROR(INDEX($AB$1:$AB$2,MATCH($A1,$AA$1:$AA$2,0)),"Other")
This Formula is in B1, while A1 contains Medina, Brunswick, etc. AA1:AA2 contain Medina...Brunswick and AAB1:AAB2 ME...BR. It can be moved to a different sheet, and you can extend it by selecting the last cell and inserting whole rows. Later you may want to use named ranges, too.

Copy part of text from the same cell

I have a problem when I downloaded a file containing different kind of information that should be stored in different cells but all is written in the same cell.
For example A:9 contains:
2016.03.16,"8982266507","QLGJG","AHGLG","OKK","IK","ODEADKIK","DK57200028982561607","485979,12","65164,94","485979,12","65164,94","485979,12","65164,94","","","","","","",
I would like to have a macro that copies specific parts of this string for example the last part "65164,94" and paste in to cell A:10.
Thank you in advance
As well as Seb's answer, you can use the split function. So:
Sub splitting_string()
Dim arr1 As Variant, var1 As String
var1 = Range("A9")
arr1 = Split(var1, ",")
For i = 0 To UBound(arr1)
Cells(10 + i, 1) = arr1(i)
Next i
End Sub
This will separate the long string in A9 into smaller ones by splitting them every time there's a comma, placing them in the cells below.

Create string in CELL B1 from multiple rows in ColumnA

I have a column in a worksheet:
ColumnA
wer
rfvg
swe
dfe
I would like to create a string 'wer','rfvg','swe','dfe' for use in a TSQL query
SELECT value
FROM table
WHERE code IN ('wer','rfvg','swe','dfe')
What I do now:
1. put CONCATENATE("'";A1;"',") in B1
2. drag it all the way down
3. copy+ paste the generated values in ColumnB into my query
What I get is this:
SELECT value
FROM table
WHERE code IN ('wer',
'rfvg',
'swe',
'dfe')
Since these last can contain up to 100 codes that is really annoying.
I would like to create a single continuous string in cell B1.
Is there a way to do this?
Thanx for thinking with me
Here is a macro to do this:
Sub BuildListForQuery
Dim oCells As Object, aCell As Object, oDoc As Object
Dim iColumnA As Integer, sList As String
oDoc = ThisComponent
iColumnA = 0
oColumn = oDoc.Sheets(0).Columns(iColumnA)
oRanges = oDoc.createInstance("com.sun.star.sheet.SheetCellRanges")
oRanges.insertByName("", oColumn)
oCells = oRanges.Cells.createEnumeration
If Not oCells.hasMoreElements Then Print "Sorry, no text to display"
While oCells.hasMoreElements
aCell = oCells.nextElement
If sList = "" Then
sList = "'" + aCell.String + "'"
Else
sList = sList + ",'" + aCell.String + "'"
End If
Wend
aCell = oDoc.Sheets(0).getCellRangeByName("B1")
aCell.setString(sList)
End Sub
Result in B1 is 'wer','rfvg','swe','dfe'.
How about this:
Put in C1 =CONCATENATE("'",A1,"'")
Put in D1 =CONCATENATE(B1,",'",INDIRECT("A" & COLUMN()-2),"'")
COLUMN returns a number for the column: A is 1, B is 2, etc.
INDIRECT takes a string entry and returns the contents of a cell address, in this case the contents of the cell in column A, row [calculated from column number]. So column D (4) looks at row 2 (4 - 2), column E (5) looks at row 3 (5 - 2), etc.
Copy and paste D1 over however many columns you want (100 columns would be to column CX)
Put in some other cell, we'll say B3 for purposes of this post, =COUNTA(A1:A100) to show how many entries you have in column A
In B1 (the cell where you want the final string) put the formula =INDIRECT(ADDRESS(1,B3+2)) - of course replace B3 with wherever you actually put the COUNTA formula
ADDRESS takes (row number, column number) - here [row 1, column (however many entries in column A + 2)] and returns a LetterNumber cell address