Double concatenation in different TextBox depending on category - vba

I don't know if somebody already asked a question about it or not, i'm new to VBA but i've already done some C# programming.
Code
in Text :
For i = 1 To 5 'Category
For j = 1 To 7 'Each entry of the category
If UserForm1.Controls("Cat" & j & "Entry" & i).Value <> "" Then
Range("A" & i).Value = UserForm1.Controls("Cat" & i & "Entry" & j).Value
End If
Next j
Next i
So this is basicaly what i'm trying to do, i have 2 categories, each one have 7 TextBox, depending on how many of them are filled, I wanted to put their values on a cell... But seems like the concatenation isn't working, also tried the For Each methods but no results..
The interface
Thank you guys

You aren't concatenating anything you are overwriting cells A1-5 every time.
For i = 1 To 5 'Category
For j = 1 To 7 'Each entry of the category
If UserForm1.Controls("Cat" & j & "Entry" & i).Value <> "" Then
if range("A" & i).value <> "" then
Range("A" & i).Value = Range("A" & i).Value & " " & UserForm1.Controls("Cat" & i & "Entry" & j).Value
else 'Avoiding the space
range("A" & i).value = Userform1.controls("Cat" & i & "Entry" & j).value
end if
End If
Next j
Next i

Related

VBA: Adding a condition to an ElseIf loop

I'd like to add one more condition to this Loop that would put "Discrepancy" into column J if there is a value in column F that is neither 0 or #VALUE. Any and all suggestions would be appreciated. Thank you, the current code is below:
Sub ERS_Vlookup()
Dim Lastrow As Long
Dim h As Long
For h = 5 To Lastrow
If IsError(ActiveSheet.Range("H" & h).Value) Or
IsError(ActiveSheet.Range("F" & h).Value) Then
ActiveSheet.Range("J" & h).Value = " "
ElseIf ActiveSheet.Range("H" & h).Value <> " " And _
ActiveSheet.Range("F" & h).Value = 0 Then
ActiveSheet.Range("J" & h).Value = "Paid"
Else
ActiveSheet.Range("J" & h).Value = "Processed Not Yet Paid"
End If
Next h
On the face of it, this should work, but I think you need to think it through and check whether these conditions are all mutually exclusive.
Sub ERS_Vlookup()
Dim Lastrow As Long
Dim h As Long
For h = 5 To Lastrow
If IsError(Range("H" & h).Value) Or IsError(Range("F" & h).Value) Then
Range("J" & h).Value = vbNullString
ElseIf Range("H" & h).Value <> vbNullString And Range("F" & h).Value = 0 Then
Range("J" & h).Value = "Paid"
ElseIf Not IsError(Range("F" & h).Value) And Range("F" & h).Value <> 0 Then
Range("J" & h).Value = "Discrepancy"
Else
Range("J" & h).Value = "Processed Not Yet Paid"
End If
Next h
End Sub
You can the use the And function to add booleans to loops
i recomend you visit this website http://www.excel-easy.com/vba/loop.html
for more information

Vb.net Excel Subtotals into variables

I've got Visual Basic Application that creates excel sheets based on the years, now in every excel sheet there will be subtotals of the given period that has been entered into the application. The total is being created by this:
oSheet.Range("F" & j + 1).Formula = "=SUBTOTAL(9,F" & summaryPosition & ":F" & j & ")"
oSheet.Range("G" & j + 1).Formula = "=SUBTOTAL(9,G" & summaryPosition & ":G" & j & ")"
oSheet.Range("H" & j + 1).Formula = "=SUBTOTAL(9,H" & summaryPosition & ":H" & j & ")"
oSheet.Range("I" & j + 1).Formula = "=SUBTOTAL(9,I" & summaryPosition & ":I" & j & ")"
oSheet.Range("J" & j + 1).Formula = "=SUBTOTAL(9,J" & summaryPosition & ":J" & j & ")"
Now i need to save these totals to a variable, but i have no idea how to get the totals into there. Anyone ever worked with Excel this way and knows how to solve this?
EDIT
I want to reference to the subtotal in the sheet, but i do not know how that works in Vb.net, any one an idea?
have you tried with
DIM var1 as oSheet.Range("F" & j + 1).Formula = "=SUBTOTAL(9,F" & summaryPosition & ":F" & j & ")"
it must get the result of that in var1

how to initialise my counter in vba excel

I have a problem with my vba project.
My workbook has 4 sheets (Draft, cky, coy and bey), in the sheet "draft i have all my data and i want to reorganise them. the columns "G" of the sheet "draft" contains the values (cky, coy and bey).
I want my macro to go through the colums and copy all the cells that have the same value and paste them in their corresponding sheet starting at the cell (A2), for exemple: i want the macro to copy all the data that have "cky" and paste it in the sheet "cky" starting at the cell A2 and so on/
Below you can see what i have done so far:
Sub MainPower()
Dim lmid As String
Dim srange, SelData, ExtBbFor As String
Dim lastrow As Long
Dim i, j, k As Integer
lastrow = ActiveSheet.Range("B30000").End(xlUp).Row
srange = "G1:G" & lastrow
SelData = "A1:G" & lastrow
For i = 1 To lastrow
If InStr(1, LCase(Range("E" & i)), "bb") <> 0 Then
Range("G" & i).Value = Mid(Range("E" & i), 4, 3)
ElseIf Left(Range("E" & i), 1) = "H" Then
Range("G" & i).Value = Mid(Range("E" & i), 7, 3)
Else
Range("G" & i).Value = Mid(Range("E" & i), 1, 3)
End If
Next i
'Sorting data
Range("A1").AutoFilter
Range(SelData).Sort key1:=Range(srange), order1:=xlAscending, Header:=xlYes
'Spreading to the appropriate sheets
j = 1
For i = 1 To lastrow
If Range("G" & i).Value = "CKY" Then
Sheets("CKY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "BEY" Then
Sheets("BEY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "COY" Then
Sheets("COY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
End If
j = j + 1
Next i
End Sub
Thank you to help
best regards
Use this refactored code in the For Loop and it should work for better for you:
For i = 1 To lastrow
Select Case Sheets("Draft").Range("G" & i).Value
Case is = "CKY","COY","BEY"
Dim wsPaste as Worksheet
Set wsPaste = Sheets(Range("G"& i).Value)
Dim lRowPaste as Long
lRowPaste = wsPaste.Range("A" & .Rows.COunt).End(xlup).Offset(1).Row
wsPaste.Range("A" & lRowPaste & ":E" & lRowPaste).Value = _
Sheets("Draft").Range("C" & i & ":G" & i).Value
End Select
Next i

VBA Macro Search Difference in Range

I need to check the range from A1 to AY1, B1 to BY1.... from sheet 1 and compare them with the range from A1 to AY1, B1 to BY1.... from sheet 2 and highlight in yellow the difference in sheet 1.
Sub copyWorkingFileToConsolidated()
Dim i As Integer
i = 5
Do While Sheet2.Range("A" & i).Value <> ""
Sheet3.Range("A" & i & ":D" & i).Value = Sheet2.Range("A" & i & ":D" & i).Value
Sheet3.Range("F" & i & ":Y" & i).Value = Sheet2.Range("E" & i & ":X" & i).Value
Sheet3.Range("A" & i & ":Y" & i).Interior.ColorIndex = 0
i = i + 1
Loop
getResourceLevel
End Sub
Please help
Thanks

Moving range of cells if a cell in the same row contain "TEXT"

Hi I need to move a range of cells in each row (scanned) that contains a cell in a specific column (could be any column in my case, but in my code it is colomn "E") which value start by the text string l. Here is what I've tried (I'm new to VBA). In this case I want to move Ji:Mi to Ki:Ni, where i is row number.
Sub Move2()
For i = 48 To 31 Step -1
If Range("E" & i) = "*l" Then
Range("J" & i & ":" & "M" & i).Cut
Range("K" & i & ":" & "N" & i).Select
ActiveSheet.Paste
End If
Next i
End Sub
Try using Like Operator like this:
Sub Move2()
For i = 48 To 31 Step -1
If Range("E" & i) Like "l*" Then
Range("J" & i & ":" & "M" & i).Cut _
Range("K" & i & ":" & "N" & i)
End If
Next i
End Sub
Above code specifically looks for l and will ignore L.
If you need to include both, use this line for the If. HTH.
If Range("E" & i) Like "[Ll]*" Then