How can it be difficult to add buttons showing previous and next entries on the userform? There is a bunch of source on the net. I've tried many of them but no go. I have tried to adapt one of the solutions mentioned in this very site, I failed. :(
In the column A there are item numbers (say 180) It may be according to 180 or to as long as it gets. I want to add the previous and next buttons. Then buttons to show the first and last entry.
My code is:
Private Sub UserForm_Initialize()
Dim k As Long, j As Long
Dim rng As Range
Set rng = Worksheets("BİLGİLER").Range("A180")
k = 0: j = 1
vyakinligi.Value = rng.Offset(k).Value
vadsoyad.Value = rng.Offset(k, j).Value: j = j + 1
vmeslegi.Value = rng.Offset(k, j).Value: j = j + 1
visadresi.Value = rng.Offset(k, j).Value: j = j + 1
vceptel.Value = rng.Offset(k, j).Value: j = j + 1
End Sub
'~~> Next Button
Private Sub CommandButton7_Click()
k = k + 1: j = 1
If k > (Sheets("BİLGİLER").Rows.Count - 4) Then
MsgBox "Max rows Reached"
Exit Sub
End If
vyakinligi.Value = rng.Offset(k).Value
vadsoyad.Value = rng.Offset(k, j).Value: j = j + 1
vmeslegi.Value = rng.Offset(k, j).Value: j = j + 1
visadresi.Value = rng.Offset(k, j).Value: j = j + 1
vceptel.Value = rng.Offset(k, j).Value: j = j + 1
End Sub
'~~> Previous Button
Private Sub CommandButton8_Click()
k = k - 1: j = 1
If k < 0 Then
MsgBox "1st Row Reached"
Exit Sub
End If
vyakinligi.Value = rng.Offset(k).Value
vadsoyad.Value = rng.Offset(k, j).Value: j = j + 1
vmeslegi.Value = rng.Offset(k, j).Value: j = j + 1
visadresi.Value = rng.Offset(k, j).Value: j = j + 1
vceptel.Value = rng.Offset(k, j).Value: j = j + 1
End Sub
Where did I go wrong? What should I do to add the buttons and show previous, next, first and the last entry on the userform?
I managed to run (to adapt) the code finally. I will put it here in case someone makes use of it.
In general tab:
Dim Data As Variant
Dim LastRow As Long
Dim r As Long
The previous and next buttons:
Private Sub CommandButton7_Click()
RangeRow xlNext
End Sub
Private Sub CommandButton8_Click()
RangeRow xlPrevious
End Sub
To userform's initialize section:
With Sheets("BİLGİLER") 'change the name as you wish
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Data = .Range("A1:AZ" & LastRow).Value 'data range
End With
r = ActiveCell.Row - 1
RangeRow r
And this Subroutine code:
Sub RangeRow(ByVal Direction As Long)
r = IIf(Direction = xlPrevious, r - 1, r + 1)
If r < 2 Then r = 2
If r > LastRow Then r = LastRow
With Me
.sira.Text = Data(r, 1)
.tckn.Text = Data(r, 2)
.oadsoyad.Text = Data(r, 3)
.cinsiyet.Text = Data(r, 4)
.dyeri.Text = Data(r, 5)
.dyili.Text = Data(r, 6)
.ncsn.Text = Data(r, 7)
.babaadi.Text = Data(r, 8)
.anneadi.Text = Data(r, 9)
.kangrb.Text = Data(r, 10)
.oceptel.Text = Data(r, 11)
.oevadresi.Text = Data(r, 12)
End With
End Sub
This code takes the info from the related row on the table and fills the userform textboxes and comboboxes on the usefrorm.
Now it's time to put buttons for the first and the last entry. Any suggestions?
Related
I am a newbie vba coder here.
I have created an .xlsm with userform. Everything works fine in my computer, but when I send the file over via email, the recipient will encounter the following issues when opening the file:
I added an event handler on Workbook_Open to automatically open the userform. When the recipient open the file, it will receive this error and Debug button returns to this line:
When Submit button of the Userform is clicked, the data is supposed to be transferred to 'ThisWorkbook' but instead it creates a new file (i guess the previous version) and paste the data there.
Can anyone help me to figure out what went wrong with my file? Thank you.
Below is my code:
Inside Workbook Event Handler:
Sub Workbook_Open()
RunForm
End Sub
Module1:
Option Explicit
Option Base 1
Sub PopulateComboBox()
Dim PaymentTerms() As String, PaymentFreq() As String, PaymentTermsAlt() As String
Dim i As Integer, j As Integer, m As Integer, n As Integer, o As Integer
j = WorksheetFunction.CountA(Sheets("Populate").Columns("A:A"))
n = WorksheetFunction.CountA(Sheets("Populate").Columns("B:B"))
ReDim PaymentTerms(j - 1) As String
ReDim PaymentFreq(n - 1) As String
ReDim PaymentTermsAlt(j - 1) As String
For i = 1 To j - 1
PaymentTerms(i) = ThisWorkbook.Sheets("Populate").Range("A2:A" & (j - 1)).Cells(i, 1)
UserForm1.ComboTerms.AddItem PaymentTerms(i)
Next i
For m = 1 To n - 1
PaymentFreq(m) = ThisWorkbook.Sheets("Populate").Range("B2:B" & (n - 1)).Cells(m, 1)
UserForm1.ComboFreq.AddItem PaymentFreq(m)
Next m
For o = 1 To j - 1
PaymentTermsAlt(o) = ThisWorkbook.Sheets("Populate").Range("A2:A" & (j - 1)).Cells(o, 1)
UserForm1.ComboTermsAlt.AddItem PaymentTermsAlt(o)
Next o
UserForm1.ComboTerms.Text = PaymentTerms(1)
UserForm1.ComboFreq.Text = PaymentFreq(1)
UserForm1.ComboTermsAlt.Text = PaymentTermsAlt(1)
End Sub
Sub RunForm()
ThisWorkbook.Sheets("Printout").Activate
UserForm1.Show
End Sub
Inside Userform:
Option Explicit
Sub CommandButton1_Click()
Application.ScreenUpdating = False
If Not IsNumeric(BasePay) Or Not IsNumeric(Interest) Then
MsgBox ("Please Enter Numeric Value for Base Pay or Interest Rate")
Exit Sub
End If
If BasePay < 0 Or Interest < 0 Then
MsgBox ("Base Pay or Interest cannot be negative value")
Exit Sub
End If
ThisWorkbook.Sheets("Printout").Range("A1") = "Prepared For " & ClientName
ThisWorkbook.Sheets("Printout").Range("O1").Value = BasePay.Text
ThisWorkbook.Sheets("Printout").Range("S2").Value = Interest.Text / 100
ThisWorkbook.Sheets("Printout").Range("L3").Value = ComboTerms.Text
ThisWorkbook.Sheets("Printout").Range("O3").Value = ComboFreq.Text
ThisWorkbook.Sheets("Printout").Range("Q2").Value = ComboTermsAlt.Text
If NewCar Then
ThisWorkbook.Sheets("Printout").Range("U2").Value = "New"
Else
ThisWorkbook.Sheets("Printout").Range("U2").Value = "Used"
End If
'----- Transfer Add-On Items to Printout Sheet ---------
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 6
For i = 1 To 9
ThisWorkbook.Sheets("Printout").Cells(k, 1).MergeArea.ClearContents
k = k + 2
Next
k = 6
For i = 10 To 18
ThisWorkbook.Sheets("Printout").Cells(k, 5).MergeArea.ClearContents
k = k + 2
Next
k = 6
For i = 19 To 27
ThisWorkbook.Sheets("Printout").Cells(k, 9).MergeArea.ClearContents
k = k + 2
Next
k = 6
For i = 28 To 36
ThisWorkbook.Sheets("Printout").Cells(k, 13).MergeArea.ClearContents
k = k + 2
Next
'---- Category 1 ------
i = 6
For j = 1 To 9
If UserForm1.Controls("Checkbox" & j).Value = True Then
ThisWorkbook.Sheets("Printout").Range("A" & i).Value = Me.Controls("Checkbox" & j).Caption
i = i + 2
Else
ThisWorkbook.Sheets("Printout").Range("A" & i).Value = ""
End If
Next j
'---- Category 2 ------
i = 6
For j = 10 To 18
If UserForm1.Controls("Checkbox" & j).Value = True Then
ThisWorkbook.Sheets("Printout").Range("E" & i).Value = Me.Controls("Checkbox" & j).Caption
i = i + 2
Else
ThisWorkbook.Sheets("Printout").Range("E" & i).Value = ""
End If
Next j
'---- Category 3 ------
i = 6
For j = 19 To 27
If UserForm1.Controls("Checkbox" & j).Value = True Then
ThisWorkbook.Sheets("Printout").Range("I" & i).Value = Me.Controls("Checkbox" & j).Caption
i = i + 2
Else
ThisWorkbook.Sheets("Printout").Range("I" & i).Value = ""
End If
Next j
'---- Category 4 ------
i = 6
For j = 28 To 36
If UserForm1.Controls("Checkbox" & j).Value = True Then
ThisWorkbook.Sheets("Printout").Range("M" & i).Value = Me.Controls("Checkbox" & j).Caption
i = i + 2
Else
ThisWorkbook.Sheets("Printout").Range("M" & i).Value = ""
End If
Next j
UserForm1.Hide
End Sub
Sub CommandButton2_Click()
Unload UserForm1
UserForm1.Show
End Sub
Sub CommandButton3_Click()
Unload UserForm1
End Sub
Sub NewCar_Click()
Dim LastRow As Integer
Dim i As Integer
Dim j As Integer
LastRow = WorksheetFunction.CountA(Sheets("Populate").Columns("D:D"))
'---- Count No of Checkbox
Dim Ctrl As MSForms.Control
Dim n As Integer
Dim cbcount As Long
For n = 0 To Me.Controls.Count - 1
If Left(UserForm1.Controls(n).Name, 8) = "CheckBox" Then
cbcount = cbcount + 1
End If
Next n
i = 2 '--- Preset counter i
For j = 1 To cbcount
UserForm1.Controls("Checkbox" & j).Caption = ThisWorkbook.Sheets("Populate").Cells(i, 4).Value
i = i + 1
If i > LastRow And i < (cbcount / 4) Then
UserForm1.Controls("Checkbox" & j).Caption = ""
End If
If i > LastRow And i > (cbcount / 4 + 1) Then
i = 2
End If
Next j
End Sub
Sub UsedCar_Click()
Dim LastRow As Integer
Dim i As Integer
Dim j As Integer
LastRow = WorksheetFunction.CountA(Sheets("Populate").Columns("D:D"))
'---- Count No of Checkbox
Dim Ctrl As MSForms.Control
Dim n As Integer
Dim cbcount As Long
For n = 0 To Me.Controls.Count - 1
If Left(Me.Controls(n).Name, 8) = "CheckBox" Then
cbcount = cbcount + 1
End If
Next n
i = 2 '--- Preset counter i
For j = 1 To cbcount
UserForm1.Controls("Checkbox" & j).Caption = ThisWorkbook.Sheets("Populate").Cells(i, 8).Value
i = i + 1
If i > LastRow And i < (cbcount / 4) Then
UserForm1.Controls("Checkbox" & j).Caption = ""
End If
If i > LastRow And i > (cbcount / 4 + 1) Then
i = 2
End If
Next j
End Sub
Sub UserForm_Initialize()
Call PopulateComboBox
'----- Rename Frame Boxes Caption
Dim k As Integer, nc As Integer
nc = 1
For k = 2 To 5
Me.Controls("Frame" & k).Caption = ThisWorkbook.Sheets("Printout").Cells(5, nc)
nc = nc + 4
Next k
'--------------------------------------------------
Dim LastRow As Integer
Dim i As Integer
Dim j As Integer
LastRow = WorksheetFunction.CountA(ThisWorkbook.Sheets("Populate").Columns("D:D"))
'---- Count No of Checkbox
Dim Ctrl As MSForms.Control
Dim n As Integer
Dim cbcount As Long
For n = 0 To Me.Controls.Count - 1
If Left(Me.Controls(n).Name, 8) = "CheckBox" Then
cbcount = cbcount + 1
End If
Next n
i = 2 '--- Preset counter i
For j = 1 To cbcount
UserForm1.Controls("Checkbox" & j).Caption = ThisWorkbook.Sheets("Populate").Cells(i, 4).Value
i = i + 1
If i > LastRow And i < (cbcount / 4) Then
UserForm1.Controls("Checkbox" & j).Caption = ""
End If
If i > LastRow And i > (cbcount / 4 + 1) Then
i = 2
End If
Next j
End Sub
I am trying to apply the bold format to all words before a colon (:) in a specific cell. In the image, the words first / second / third need to be in bold, the rest not.
I found the following code on a different thread, but it applies the bold format to the first word before a colon.
Sub PreColon()
Dim i As Long, N As Long, s As String, j As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To N
s = Cells(i, 1)
j = InStr(1, s, ":")
If j <> 0 Then
Cells(i, 1).Characters(1, j - 1).Font.Bold = True
End If
Next i
End Sub
split on the - and do a second loop:
Sub PreColon()
With ActiveSheet
Dim N As Long
N = .Cells(.Rows.Count, "A").End(xlUp).Row
Dim i As Long
For i = 1 To N
Dim strLen As Long
strLen = 0
Dim sArray() As String
sArray = Split(.Cells(i, 1), "-")
Dim s As Variant
For Each s In sArray
Dim j As Long
j = InStr(s, ":")
If j > 0 Then
.Cells(i, 1).Characters(strLen + 1, j - 1).Font.Bold = True
End If
strLen = strLen + Len(s) + 1
Next s
Next i
End With
End Sub
Here is a little procedure you can use:
Sub Test()
Dim i As Long, N As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To N
FormatPreColon Cells(i, 1)
Next
End Sub
Sub FormatPreColon(Rng As Range)
Dim i As Long, j As Long
If TypeName(Rng.Value) <> "String" Then Exit Sub
i = InStr(1, Rng, ":")
Do While i <> 0
j = InStrRev(Rng, " ", i) + 1
Rng.Characters(j, i - j).Font.Bold = True
i = InStr(i + 1, Rng, ":")
Loop
End Sub
Possible missing "-" symbol you may use this.
Dim i As Long, s As String, j As Integer, k As Integer, t As String, counter As Integer, N As Integer
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To N
s = Cells(i, 1)
j = 1
k = 1
Do While j > 0
j = InStr(k, s, ":")
k = j + 1
counter = 1
For m = j - 1 To 1 Step -1
t = Trim(Mid(s, m, 1))
If (t = "" Or m = 1) Then
Cells(i, 1).Characters(m, counter).Font.Bold = True
Exit For
Else
counter = counter + 1
End If
Next m
Loop
DoEvents
Next i
MsgBox "Finito..."
I have a worksheet with over 60,000 rows and two columns. One column is transaction id, the other is item. I want to find the combinations of items in the orders. I found this vba code from someone with a similar problem
Sub basket()
On Error Resume Next
Dim ps(2, 20)
r = 3
tr = Cells(2, 1)
Item = Cells(2, 2) + "."
ps(1, 1) = 1
ps(2, 1) = Len(Item)
r2 = 2
r3 = 3
ic = 2
While Cells(r, 1) <> ""
If Cells(r, 1) <> tr Then
o = 1
k = 1
If ic > 1 Then
ic = ic - 1
While o = 1
For i = 1 To ic
entry = Mid(Item, ps(1, i), ps(2, i))
For j = i + k To ic
entry = entry & Mid(Item, ps(1, j), ps(2, j))
Cells(r2, 10) = tr
Cells(r2, 11) = entry
r2 = r2 + 1
x = 0
x = Application.WorksheetFunction.Match(entry, Range("e:e"), 0)
If x = 0 Then
x = r3
Cells(x, 5) = entry
r3 = r3 + 1
End If
Cells(x, 6) = Cells(x, 6) + 1
Next j
Next i
If k > Len(Item) - 1 Then o = 0
k = k + 1
Wend
End If
Item = ""
ic = 1
tr = Cells(r, 1)
End If
ps(1, ic) = Len(Item) + 1
ps(2, ic) = Len(Cells(r, 2)) + 1
Item = Item + Cells(r, 2) + "."
r = r + 1
ic = ic + 1
Wend
o = 1
k = 1
If ic > 1 Then
ic = ic - 1
While o = 1
For i = 1 To ic
entry = Mid(Item, ps(1, i), ps(2, i))
For j = i + k To ic
entry = entry & Mid(Item, ps(1, j), ps(2, j))
Cells(r2, 10) = tr
Cells(r2, 11) = entry
r2 = r2 + 1
x = 0
x = Application.WorksheetFunction.Match(entry, Range("e:e"), 0)
If x = 0 Then
x = r3
Cells(x, 5) = entry
r3 = r3 + 1
End If
Cells(x, 6) = Cells(x, 6) + 1
Next j
Next i
If k > Len(Item) - 1 Then o = 0
k = k + 1
Wend
End If
End Sub
Which worked when I ran the exact same code but with item categories. The problem is I'm running it with the item names and it's always crashing my Excel. Is there anyone that can guide me in the right direction?
this is the worksheet that doesn't work
this is what I get when I run it with the item category which works. They're the exact same data, one just has it as item category, and the other is item name.
Your code sample didn't do anything for me. It ran, but it didn't actually produce any kind of results at all. I did a quick Google search and found this.
Sub ListCombinations()
Dim col As New Collection
Dim c As Range, sht As Worksheet, res
Dim i As Long, arr, numCols As Long
Set sht = ActiveSheet
'lists begin in A1, B1, C1, D1
For Each c In sht.Range("A2:B2").Cells
col.Add Application.Transpose(sht.Range(c, c.End(xlDown)))
numCols = numCols + 1
Next c
res = Combine(col, "~~")
For i = 0 To UBound(res)
arr = Split(res(i), "~~")
sht.Range("H1").Offset(i, 0).Resize(1, numCols) = arr
Next i
End Sub
'create combinations from a collection of string arrays
Function Combine(col As Collection, SEP As String) As String()
Dim rv() As String
Dim pos() As Long, lengths() As Long, lbs() As Long, ubs() As Long
Dim t As Long, i As Long, n As Long, ub As Long
Dim numIn As Long, s As String, r As Long
numIn = col.Count
ReDim pos(1 To numIn)
ReDim lbs(1 To numIn)
ReDim ubs(1 To numIn)
ReDim lengths(1 To numIn)
t = 0
For i = 1 To numIn 'calculate # of combinations, and cache bounds/lengths
lbs(i) = LBound(col(i))
ubs(i) = UBound(col(i))
lengths(i) = (ubs(i) - lbs(i)) + 1
pos(i) = lbs(i)
t = IIf(t = 0, lengths(i), t * lengths(i))
Next i
ReDim rv(0 To t - 1) 'resize destination array
For n = 0 To (t - 1)
s = ""
For i = 1 To numIn
s = s & IIf(Len(s) > 0, SEP, "") & col(i)(pos(i)) 'build the string
Next i
rv(n) = s
For i = numIn To 1 Step -1
If pos(i) <> ubs(i) Then 'Not done all of this array yet...
pos(i) = pos(i) + 1 'Increment array index
For r = i + 1 To numIn 'Reset all the indexes
pos(r) = lbs(r) ' of the later arrays
Next r
Exit For
End If
Next i
Next n
Combine = rv
End Function
I found that from this link.
VBA - Write all possible combinations of 4 columns of data
I'm pretty sure if you do some more Googling, you can find other concepts that do pretty much the same thing.
I have a current code that is supposed to compare tables in sheet1 to sheet2 and output the matches in another sheet(sheet3). When trying to run the code, I am getting "Type mismatch" error" and I'm not sure what is wrong nor how to fix it...
Picture of VBA code and the second table/sheet:
Sub CompareSolve()
Dim i As Long
Dim j As Long
Dim n As Long
Dim ar As Variant
ar = Sheet2.Cells(10, 1).CurrentRegion.Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 2 To UBound(ar, 1)
.Item(ar(i, 1)) = Empty
Next
ar = Sheet1.Cells(10, 1).CurrentRegion.Value
n = 1
For i = 2 To UBound(ar, 1)
If .exists(ar(i, 1)) Then
n = n + 1
For j = 1 To UBound(ar, 2)
ar(n, j) = ar(i, j)
Next j
End If
Next i
End With
Sheet3.Cells(10, 8).Resize(n, UBound(ar, 2)).Value = ar
End Sub
These two tables are in the same position on the sheets for these 2 sheets
Fixed code thanks to help from #Tom
Dim i As Long
Dim j As Long
Dim n As Long
Dim ar As Variant
ar = Sheet2.Cells(10, 1).CurrentRegion.Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 2 To UBound(ar, 1)
.Item(ar(i, 1)) = Empty
Next
ar = Sheet1.Cells(10, 1).CurrentRegion.Value
n = 1
For i = 2 To UBound(ar, 1)
If .exists(ar(i, 1)) Then
n = n + 1
For j = 1 To UBound(ar, 2)
ar(n, j) = ar(i, j)
Next j
End If
Next i
End With
Sheet3.Cells(10, 8).Resize(n, UBound(ar, 2)).Value = ar
End Sub```
I'm new on VBA and i need some help. I've found this code and i adapted it to my needs, but the issue is that i can't copy the first 100 cells to the next column on the same row in a table(the column E is already filled and i want to paste the values to the column F).
here is the code:
Sub variable_to_check()
Dim j As Integer, r As Range, k As Integer, dest As Range
j = 100
With Worksheets("Calibrari")
Set r = .Range("A2")
k = 0
Do
Range(r, r.Offset(j - 1, 0)).copy
With Worksheets("INCA")
Set dest = .Cells(Rows.count, "F").Offset(0, 0).End(xlUp).Offset(0, -1)
dest.PasteSpecial
'this add the text "INCA_Read" in the first column after each 100 cells
lr = ActiveSheet.Cells(Rows.count, "E").End(xlUp).Row + 1
ActiveSheet.Cells(lr, "A").value = "INCA_Read"
If k < .Range("F13").Column - 2 Then
k = k + 1
Else
k = 0
End If
End With
Set r = r.Offset(j, 0)
If r = .Range("A2").End(xlDown).Offset(1, 0) Then Exit Do
Loop
End With
ThisWorkbook.Worksheets("INCA").Cells.EntireColumn.AutoFit
End Sub
Sub value_to_be_checked() <--in this macro i think the issue is
Dim j As Integer, r As Range, k As Integer, dest As Range
j = 100
With Worksheets("Calibrari")
Set r = .Range("C2")
k = 0
Do
Range(r, r.Offset(j - 1, 0)).copy
With Worksheets("INCA")
Set dest = .Cells(Rows.count, "E").Offset(0, 0).End(xlUp).Offset(0, 1)
dest.PasteSpecial
If k < .Range("E13").Column - 2 Then
k = k + 1
Else
k = 0
End If
End With
Set r = r.Offset(j, 0)
If r = .Range("C2").End(xlDown).Offset(1, 0) Then Exit Do
Loop
End With
End Sub
Thanks!
Instead of using Copy/Paste, just set the value of the cell:
Worksheets("INCA").Cells(Rows.count, "E").End(xlUp).Offset(0, 1) = Range(r, r.Offset(j - 1, 0)).Value2