Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all 50319 articles
Browse latest View live

Conditional drawing

$
0
0
excel13.jpeg

I have this column (A1:A500) with some numbers. I wonder, is it possible that Excel can draw a red circle after detecting any cell including the number 500 (or any specific text) ?

Thank you very much everybody.

Using multiple TextBoxes to match if the line in sheet exists

$
0
0
Hi There Experts
Trust all is well,

I know that someone here will be able to assist me.

I Basically created a UserForm that Populates a Sheet.

Now I have made a Private Sub linked to a button to lookup if data already exists in sheet.

I have 3 different Criteria IF Functions that I need to match in order to update the sheet in different ways.

So I have a Sheet that Starts on "B3" and ends at "AA3"

So with My userform I need to have a match or lookup function to see if Me.TX_1.Value = Sheets("Sheet1").Range("B:B") as the first criteria.

Then I need to look at Criteria 1 and add and Me.TX_2.Value = Sheets("Sheet1").Range("N:N") and then for 3rd Criteria add and Me.TX_3.Value = Sheets("Sheet1").Range("R:R") .

My Code Is below. I basically want to know if the line exists with Criteria 1 and then update only the cells in that row that does not match with the Search Criteria.

I need to Match the Data if it exists in the Sheet on single row and then populate my Userform with it and then make changes to it based on what is needed and then only update the changes.

The Update Sub is working but need to try and Match data before submitting it back to overright the line on the sheet.

Code:

Private Sub CB_SITE_ID_Click()
Dim i As Integer, answer As VbMsgBoxResult, lastrow As Long, x As Long

lastrow = Sheets("VVS Installations").Cells(Rows.Count, "B").End(xlUp).row

For x = 4 To lastrow
For i = 4 To lastrow

If Me.TX_TYPE_OF_FEEDBACK <> "" Then

    If Me.TX_1.Value <> "" Then
   
        If Sheets("VVS Installations").Cells(x, "B") = Me.TX_1.Value And Sheets("VVS Installations").Cells(x, "N") = 1 _
        And Sheets("VVS Installations").Cells(x, "R") = 3 Then
       
            Me.Controls("TX_1").Value = Worksheets("VVS Installations").Cells(i, "B")
            Me.Controls("TX_2").Value = Worksheets("VVS Installations").Cells(i, "C")
            Me.Controls("TX_3").Value = Worksheets("VVS Installations").Cells(i, "D")
            Me.Controls("TX_4").Value = Worksheets("VVS Installations").Cells(i, "E")
            Me.Controls("TX_5").Value = Worksheets("VVS Installations").Cells(i, "F")
            Me.Controls("TX_6").Value = Worksheets("VVS Installations").Cells(i, "G")
            Me.Controls("TX_7").Value = Worksheets("VVS Installations").Cells(i, "H")
            Me.Controls("TX_8").Value = Worksheets("VVS Installations").Cells(i, "I")
            Me.Controls("TX_9").Value = Worksheets("VVS Installations").Cells(i, "J")
            Me.Controls("TX_10").Value = Worksheets("VVS Installations").Cells(i, "K")
            Me.Controls("TX_DATE_ATTENDED") = Worksheets("VVS Installations").Cells(i, "L")
            Me.Controls("TX_NVR_QTY").Value = 1
            Me.Controls("TX_SWITCH_QTY").Value = 1
            Me.Controls("TX_SURGE_QTY").Value = 3
            Me.Controls("TX_UNABLE_TO_INSTALL_MAINTAIN").Value = "NO"
            Me.Controls("TX_DOME_CAM_QTY").Value = Worksheets("VVS Installations").Cells(i, "N")
            Me.Controls("TX_TEAM").Value = Worksheets("VVS Installations").Cells(i, "O")
            Me.Controls("TX_BULLET_CAM_QTY").Value = Worksheets("VVS Installations").Cells(i, "R")
            Me.Controls("TX_IP_RANGE_START").Value = Worksheets("VVS Installations").Cells(i, "T")
            Me.Controls("TX_GATEWAY").Value = Worksheets("VVS Installations").Cells(i, "U")
            Me.Controls("TX_CTN_PORT").Value = Worksheets("VVS Installations").Cells(i, "W")
            Me.Controls("TX_PLATFORM").Value = Worksheets("VVS Installations").Cells(i, "X")
            Me.Controls("TX_SITE_LIVE_DD").Value = Worksheets("VVS Installations").Cells(i, "Y")
            Me.Controls("TX_SITE_VANDALIZED").Value = Worksheets("VVS Installations").Cells(i, "Z")
            Me.Controls("TX_COMMENT").Value = Worksheets("VVS Installations").Cells(i, "AA")
           
            MsgBox "The site has already been installed with Container and Tower Cameras. Please use maintenance to update the site Details."
            Me.TX_SITE_ID_CHECK = "Exist"
            Exit Sub
       
        ElseIf Sheets("VVS Installations").Cells(x, "B") = Me.TX_1.Value And Sheets("VVS Installations").Cells(x, "N") = 1 _
        And Sheets("VVS Installations").Cells(x, "R") = 0 Then
       
            Me.Controls("TX_1").Value = Worksheets("VVS Installations").Cells(i, "B")
            Me.Controls("TX_2").Value = Worksheets("VVS Installations").Cells(i, "C")
            Me.Controls("TX_3").Value = Worksheets("VVS Installations").Cells(i, "D")
            Me.Controls("TX_4").Value = Worksheets("VVS Installations").Cells(i, "E")
            Me.Controls("TX_5").Value = Worksheets("VVS Installations").Cells(i, "F")
            Me.Controls("TX_6").Value = Worksheets("VVS Installations").Cells(i, "G")
            Me.Controls("TX_7").Value = Worksheets("VVS Installations").Cells(i, "H")
            Me.Controls("TX_8").Value = Worksheets("VVS Installations").Cells(i, "I")
            Me.Controls("TX_9").Value = Worksheets("VVS Installations").Cells(i, "J")
            Me.Controls("TX_10").Value = Worksheets("VVS Installations").Cells(i, "K")
            Me.Controls("TX_DATE_ATTENDED") = Worksheets("VVS Installations").Cells(i, "L")
            Me.Controls("TX_NVR_QTY").Value = 1
            Me.Controls("TX_SWITCH_QTY").Value = 1
            Me.Controls("TX_SURGE_QTY").Value = 0
            Me.Controls("TX_UNABLE_TO_INSTALL_MAINTAIN").Value = "NO"
            Me.Controls("TX_DOME_CAM_QTY").Value = Worksheets("VVS Installations").Cells(i, "N")
            Me.Controls("TX_TEAM").Value = Worksheets("VVS Installations").Cells(i, "O")
            Me.Controls("TX_BULLET_CAM_QTY").Value = Worksheets("VVS Installations").Cells(i, "R")
            Me.Controls("TX_IP_RANGE_START").Value = Worksheets("VVS Installations").Cells(i, "T")
            Me.Controls("TX_GATEWAY").Value = Worksheets("VVS Installations").Cells(i, "U")
            Me.Controls("TX_CTN_PORT").Value = Worksheets("VVS Installations").Cells(i, "W")
            Me.Controls("TX_PLATFORM").Value = Worksheets("VVS Installations").Cells(i, "X")
            Me.Controls("TX_SITE_LIVE_DD").Value = Worksheets("VVS Installations").Cells(i, "Y")
            Me.Controls("TX_SITE_VANDALIZED").Value = Worksheets("VVS Installations").Cells(i, "Z")
            Me.Controls("TX_COMMENT").Value = Worksheets("VVS Installations").Cells(i, "AA")
           
            MsgBox "The site has Container Camera installed."
            Me.TX_SITE_ID_CHECK = "Exist"
            Exit Sub
       
        Else
        MsgBox "There is no record for an installation on the given Site ID"
        Me.TX_SITE_ID_CHECK = "Valid"
        Exit Sub
        End If
   
    Else
    MsgBox "No Site ID Entered"
    Exit Sub
    End If

Else
MsgBox " Please select a Feedback type."
Exit Sub
End If
Next i
Next x

End Sub

Make code quicker

$
0
0
I have a simple code that copies and pastes a row from above to the new row. The Issue is its VERY SLOW. How can i re-write it to copy and paste to the right columns B:AX? Instead of the entire column?

Code:

Sub InsertCopyRow()

    Set rng = ActiveCell
    Application.ScreenUpdating = False
   
    ActiveSheet.Unprotect Password:="nro!!!"
    ActiveCell.Offset(1, 0).EntireRow.Insert
    ActiveCell.EntireRow.Copy ActiveCell.Offset(1, 0).EntireRow
    ActiveCell.FormulaR1C1 = "ENTER REF #"
    ActiveSheet.Protect Password:="nro!!!", AllowFormattingCells:=True
   
    Application.ScreenUpdating = True
   
End Sub

Can't initiate Outlook 'Send/Receive All Folders' from Excel

$
0
0
Hi Guys,

I really need some help here. My situation is that I need to use a computer which has Outlook 2016 with the 'Send immediately when connected' option unticked (so all emails go to the Outbox when they're initially sent) and I can't do anything about that.

I'm trying to write a really simple macro that performs the following steps:

1) Construct an email message from within Excel
2) Transfer the message from Excel to Outlook 2016 and 'send' it from the default email account
3) Move it from the Outbox to the Sent Items folder (i.e. properly sending it)

All of my somewhat limited VBA experience has been within the Excel application and I'm not really familiar with automation of other apps. I've borrowed code from other threads (many thanks!) and I've managed to complete steps 1 & 2 with no problems. However, I'm stuck on step 3.

I've obtained some code for this which I thought would work but it just doesn't seem to do anything at all. When I step through the code line by line, I don't get any error whatsoever but the email that was created and sent at steps 1 and 2 just remains stuck in the Outbox. The same is true whether Outlook is closed or running at the start of the macro.

Does anybody have any ideas as to where I'm going wrong? I'd really appreciate you taking a look - my code is as follows:

Code:

Sub Send_Email()

Dim myApp As Object
Dim NewEmail As Object
Dim mySyncObjects As Object
Dim ItemCounter As Long

Set myApp = CreateObject("Outlook.Application")
Set NewEmail = myApp.CreateItem(0)
           
With NewEmail
    .To = "email address"  'Note, address format modified due to posting rules
    .Subject = "Test Email"
    .Body = "Blah, blah, blah"
    .DeleteAfterSubmit = True
    .send
End With

'-----------------------------------------------------------------------------------
'STEP 3: Initiate Send/Receive

Set mySyncObjects = myApp.GetNamespace("MAPI").SyncObjects

For ItemCounter = 1 To mySyncObjects.Count
    mySyncObjects.Item(ItemCounter).Start
Next
'-----------------------------------------------------------------------------------

End Sub

search for multiple words

$
0
0
Hello everyone!

I wrote a VBA program to search column A for the word "hello" in a table. As soon as the word is found in the column, the program transfers the row in the column to the right to a new table. This works fine. Now I want to search for several words at once. Unfortunately, this doesn't work ... Can someone look at the VBA code and tell me what I did wrong? Many thanks in advance!

The Code

Sub test()
T = "T1"
X = "A"
AX = 1

Z = "T4"
Y = 2
AY = "B"

Do Until Suche <> ""
Suche = ("hello"; "my"; "another")

Loop
Set A = Worksheets(T)
Set B = Worksheets(Z)
Y = Y

With A.Columns(X)
Set Gefunden = .Find(Suche, LookIn:=xlValues)
If Not Gefunden Is Nothing Then
Erste = Gefunden.Address
Do 'für alle Fundstellen
B.Cells(Y, AY).Resize(1, AX) = Gefunden.Offset(0, 1).Resize(1, AX).Value
Y = Y + 1
Set Gefunden = .FindNext(Gefunden)
Loop Until Gefunden.Address = Erste
End If
End With
End Sub

Variable link based on the value of a cell.

$
0
0
Hello, trying to solve a problem which I am facing right now.
Description:
I have a cell that contains the result of a formula, whose values are known (in the form like T111 / T112 /T113 / T121 / T122 and so on).
I would like each of the results in that cell to become a different link, which should change accordingly to the values displayed.
So when the result is T111, the link should aim to a specific file called T111. When the result is T112, the link should aim to a specific file called T112.

Is this something that can be done in excel without macros? Is this something that can be done only with macros? Is this something that cannot be done in excel?

Every help, hint, or thought will be much appreciated.
Dario.

vba for copy a file

$
0
0
Hello

I have this very simple vba to copy file, how can i change dest folder to be ThisWorkbook.Path and folder 11 FDV , if not exsist create

Code:

Sub test_1()
    'Copy a file
    FileCopy "O:\EMV FDV\Oljet trehåndløper\HMS_Datablad_Liberon.pdf", "C:\Users\Geir Olav\Desktop\TestFDV\HMS_Datablad_Liberon.pdf"
    End Sub

Auto-save copies twice a day at specified times to specified folder with date/timestamp

$
0
0
Hey everyone,
I am very new to VBA and trying to program my excel sheet to save itself, everyday at 6:30am and 6:30pm, to a folder outside the current file destination, with the date and time stamp. This sheet will be left open at all times for our guys out on the line to use. I have tried putting in different times in timevalue to get it to run so I can see, but nothing happens.
I am not sure why it isn't working. Does anyone see any issues with this code?

This is what I have so far in the 'ThisWorkbook' - not in the ActiveSheet.

Code:

Option Explicit

Sub AutoSaveBook1()

    Application.OnTime TimeValue("06:30:00"), "SaveBook1", TimeValue("06:30:30"), schedule:=True
 
End Sub

Sub SaveBook1()
    ActiveWorkbook.SaveAs "M:\Coating Public\Line 4 Preventative Maintenance Files\Line 4 PM Entry Historical Data\Line 4 PM Entry " & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".xlsm"
   
End Sub

Sub AutoSaveBook2()

    Application.OnTime TimeValue("18:30:00"), "SaveBook2", TimeValue("18:30:30"), schedule:=True
 
End Sub

Sub SaveBook2()
    ActiveWorkbook.SaveAs "M:\Coating Public\Line 4 Preventative Maintenance Files\Line 4 PM Entry Historical Data - " & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".xlsm"
   
End Sub

Thanks for your help.

Show Elapsed Time (in realtime) On UserForm While Marco Runs

$
0
0
Hello All!!!

I'm trying to wrap my brain around a little problem I'm having. I've create a code to automate some processes in an excel workbook for my office, however it tends to take a little while to iterate and complete the process.

To keep any users of the workbook from freaking out and thinking excel is hanging on them, I've also implemented a progress bar on a Userform.
Untitled.png

While I've got the '% Complete' section figured out, I am trying to also show the elapsed time it is taking to run the macro. My current code simply updates the userform with the elapsed time for the procedure, but I would like to have it constantly updating the elapsed time. Despite countless searching, I can't find anything that gives me insight into how you show the elapsed time in real-time while also letting the macro run in the background.

Any help with this issue would be greatly appreciated. I've attached some of my code in hopes someone out there can help. Thanks in advance!!!

Code:

'---------------------------------------'
'-----Declaring Global Variables-----'
'---------------------------------------'
Option Explicit
Dim CurrentProgress As Double
Dim ProgressPercentage As Double
Dim BarWidth As Long
Dim StartingTime As Single
Dim IterateAgain As Double

'-----------------------------------'
'-----Optimal Savings Macro-----'
'-----------------------------------'
Sub OptimalSavingsCalc()
    StartingTime = Timer
    Call InitProgressBar
    SavingsAnalysis "OptOnThresh", "OptOffThresh", "OptESUGoalSeek", "OptSOCGoalSeek", "ESS_Power_Savings", "ESSPower", "ESSCap", "NeededThresh", "Opt"
    ProgressUpdate (0.01)
End Sub

'--------------------------------------'
'-----Progress Bar Initialization-----'
'--------------------------------------'
Sub InitProgressBar()
    CurrentProgress = 0
    IterateAgain = 0
    Progress.StartUpPosition = 0
    Progress.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Progress.Width)
    Progress.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Progress.Height)
    With Progress
        .Bar.Width = 0
        .Text.Caption = "0% Complete"
        .Time.Caption = "Time Elapsed: 00:00:00"
        .Show vbModeless
    End With
End Sub

'--------------------------------'
'-----Progress Bar Update-----'
'--------------------------------'
Sub ProgressUpdate(Percentage)
    CurrentProgress = CurrentProgress + Percentage
    BarWidth = Progress.Border.Width * CurrentProgress
    ProgressPercentage = Round(CurrentProgress * 100, 0)
    Progress.Bar.Width = BarWidth
    Progress.Time.Caption = "Time Elapsed: " & Format((Timer - StartingTime) / 86400, "hh:mm:ss")
    Progress.Text.Caption = ProgressPercentage & "% Complete"
    DoEvents
End Sub

Calculate entire worksheet if certain cells are re-evaluated

$
0
0
I have a worksheet that contains a large number of cells with a UDF declaration. I would like the UDF to only be run when certain cell values, which contain direct user inputs, are changed. The UDF uses these inputs to calculate a value and also uses as inputs other cells that contain formula evaluations. I would like the UDF to run when these evaluation cells are updated as well however I can't find a way to do that without making the UDF volatile which causes some other problems with a separate function.

Searching on the internet I've seen a lot of people suggest something along the lines of this:

Code:

Private Sub Worksheet_Calculate()
    Dim Xrg As Range
    Set Xrg = Range("C2:C8")
    If Not Intersect(Xrg, Range("C2:C8")) Is Nothing Then
    Macro1
    End If
End Sub

but that seems to run when any cell on the worksheet is evaluated rather than just cells in range C2:C8.

What I have right now is the following (which can also be found in the attached file):

In the sheet object:

Code:

Private Sub Worksheet_Change(ByVal target As Range)

    Dim KeyCells As Range
    Set KeyCells = Worksheets("Sheet2").Range("Inputs")
   
    If Not Application.Intersect(KeyCells, Range(target.Address)) Is Nothing Then
        Worksheets("Sheet2").Calculate
    End If

End Sub

In the Module:

Code:

Public Function doThisThing(ByVal target As Range) As Variant

    If target.Value2 < 50 Then
        doThisThing = Worksheets("Sheet2").Cells(3, 2).Value * WorksheetFunction.Index(Worksheets("Sheet2").Range("Options"), 2, 2)
    Else
        doThisThing = Worksheets("Sheet2").Cells(3, 2).Value * WorksheetFunction.Index(Worksheets("Sheet2").Range("Options"), 3, 2)
    End If

End Function

The function "doThisThing" is called in certain cells in Sheet2.
Attached Files

=WORKDAY.INTL need help with weekend

$
0
0
I have the following problem with the table below: In column “F” I have a formula that takes the effective day and adds two business days to it. I am using the following formula: =WORKDAY.INTL(E2,2,1,H2:H17)
To see what is due today, I filter that column (“F”) to today.
The problem is, it works well most days, but:
Tuesdays it pulls Friday, Saturday and Sunday (which is correct), and then Wednesdays it gives me what was effective on Mondays,
The problem is, I need it to include Saturday and Sunday on Wednesdays (anything ordered over the weekend should be due two days after).

A B C D E F
1 Name Number Location Ordered date Effective Date Due (Eff+2 days)
2 Doe, John 12345 West Hall 1/22/2020 1/26/2020 1/28/2020
3 Doe, Jane 56789 Triaging 1/22/2020 1/26/2020 1/28/2020


Is it possible to do that?

Thanks!

How to get Command Button on UserForm to change color while code processes.

$
0
0
I have a workbook in which we track daily inspections of our facility. To make the "findings" easier to input, I have a UserForm titled "RecordForm" that opens when requested where all the findings can be input. Once everything is input into the form, you save the record by clicking an add button (the cmdAdd button in the code) and it begins copying all the data to a separate worksheet within the workbook. Once everything is copied, it clears all the entry fields and is ready for the next entry. My problem is that because of the combinations of inspectors, areas, and possible issues they might find, it takes a fair amount of time to copy everything from the userform to the worksheet, but since there is no significant change to the Userform or the Add Button, the user may not realize that it is already working and keep pressing it.

I'd like to have the "Add Record" button change from the standard gray to red while everything copies and once its done, go back to gray. Unfortunately, everything is copying over to the "Historical Data" worksheet before the button changes color at the very end. So, except for having a colorful button that I don't want at the end, it isn't accomplishing what I want it to do. Here's the code I've tried with the attempt commented out. Is it possible to do what I'm hoping for and if so, what am I doing wrong. I'm a novice with VBA so I try and find someone else's code that does what I want and copy and modify it to work on my workbook. Here's what I've got for this function.

Private Sub cmdAdd_Click()
'Copy input values to data sheet

'RecordForm.cmdAdd.BackColor = RGB(255, 0, 0)
'cmdAdd.Visible = True

Dim lRow As Long
Dim ws As Worksheet
Dim Error1 As Error

Set ws = Worksheets("Historical Data")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.MonthView1.Value
.Cells(lRow, 2).Value = Me.cboDepartment.Value
.Cells(lRow, 3).Value = Me.cboWalkArea.Value
.Cells(lRow, 4).Value = Me.txtPossibleChecks.Value
.Cells(lRow, 5).Value = Me.txtNoChecks.Value
.Cells(lRow, 6).Value = Me.cboSupervisor1.Value
.Cells(lRow, 7).Value = Me.cboSupervisor2.Value
.Cells(lRow, 8).Value = Me.cboSupervisor3.Value
.Cells(lRow, 9).Value = Me.cboSupervisor4.Value
.Cells(lRow, 10).Value = Me.cboAuditor1.Value
.Cells(lRow, 11).Value = Me.txtTime.Value
.Cells(lRow, 12).Value = Me.cboIssueFound1.Value
.Cells(lRow, 13).Value = Me.txtCommentbox.Value
.Cells(lRow, 14).Value = Me.CorrY1.Value
.Cells(lRow, 15).Value = Me.CorrN8.Value
.Cells(lRow, 16).Value = Me.TextBox2.Value
.Cells(lRow, 17).Value = Me.CheckBox8.Value
.Cells(lRow, 18).Value = Me.cboAuditor2.Value
.Cells(lRow, 19).Value = Me.txtTime2.Value
.Cells(lRow, 20).Value = Me.cboIssueFound2.Value
.Cells(lRow, 21).Value = Me.txtCommentbox2.Value
.Cells(lRow, 22).Value = Me.CorrY2.Value
.Cells(lRow, 23).Value = Me.CorrN9.Value
.Cells(lRow, 24).Value = Me.TextBox3.Value
.Cells(lRow, 25).Value = Me.CheckBox2.Value
.Cells(lRow, 26).Value = Me.cboAuditor3.Value
.Cells(lRow, 27).Value = Me.txtTime3.Value
.Cells(lRow, 28).Value = Me.cboIssueFound3.Value
.Cells(lRow, 29).Value = Me.txtCommentbox3.Value
.Cells(lRow, 30).Value = Me.CorrY3.Value
.Cells(lRow, 31).Value = Me.CorrN10.Value
.Cells(lRow, 32).Value = Me.TextBox4.Value
.Cells(lRow, 33).Value = Me.CheckBox3.Value
.Cells(lRow, 34).Value = Me.cboAuditor4.Value
.Cells(lRow, 35).Value = Me.txtTime4.Value
.Cells(lRow, 36).Value = Me.cboIssueFound4.Value
.Cells(lRow, 37).Value = Me.txtCommentbox4.Value
.Cells(lRow, 38).Value = Me.CorrY4.Value
.Cells(lRow, 39).Value = Me.CorrN11.Value
.Cells(lRow, 40).Value = Me.TextBox5.Value
.Cells(lRow, 41).Value = Me.CheckBox4.Value
.Cells(lRow, 42).Value = Me.cboAuditor5.Value
.Cells(lRow, 43).Value = Me.txtTime5.Value
.Cells(lRow, 44).Value = Me.cboIssueFound5.Value
.Cells(lRow, 45).Value = Me.txtCommentbox5.Value
.Cells(lRow, 46).Value = Me.CorrY5.Value
.Cells(lRow, 47).Value = Me.CorrN12.Value
.Cells(lRow, 48).Value = Me.TextBox6.Value
.Cells(lRow, 49).Value = Me.CheckBox5.Value
.Cells(lRow, 50).Value = Me.cboAuditor6.Value
.Cells(lRow, 51).Value = Me.txtTime6.Value
.Cells(lRow, 52).Value = Me.cboIssueFound6.Value
.Cells(lRow, 53).Value = Me.txtCommentbox6.Value
.Cells(lRow, 54).Value = Me.CorrY6.Value
.Cells(lRow, 55).Value = Me.CorrN13.Value
.Cells(lRow, 56).Value = Me.TextBox7.Value
.Cells(lRow, 57).Value = Me.CheckBox8.Value
.Cells(lRow, 58).Value = Me.cboAuditor7.Value
.Cells(lRow, 59).Value = Me.txtTime7.Value
.Cells(lRow, 60).Value = Me.cboIssueFound7.Value
.Cells(lRow, 61).Value = Me.txtCommentbox7.Value
.Cells(lRow, 62).Value = Me.CorrY7.Value
.Cells(lRow, 63).Value = Me.CorrN14.Value
.Cells(lRow, 64).Value = Me.TextBox8.Value
.Cells(lRow, 65).Value = Me.CheckBox7.Value
If cboIssueFound1.Value = "" Then
.Cells(lRow, 13).Value = "No Issues Found"
Else
.Cells(lRow, 12).Value = Me.cboIssueFound1.Value
End If

If OptionButton1.Value = True Then
.Cells(lRow, 66).Value = "Yes"
Else
.Cells(lRow, 66).Value = "No"
End If
If OptionButton3.Value = True Then
.Cells(lRow, 67).Value = "Yes"
Else
.Cells(lRow, 67).Value = "No"
End If

MsgBox "All Items Copied - Proceed"

End With[/INDENT][/INDENT]

Filtering list with userform checkboxes

$
0
0
I am currently trying to create a userform that will help to filter my list (link) based on what fruits are talked about in which documents. [Click here to see the list] [2]

For example, looking at my userform below, I would like to be able to click on apples and then click ' Find Documents' in order to filter the excel list to only the documents that talk about apples (543, 45, 723, 44, 86, 95, 7, 33, 64).

The only way I could figure out how to do was to manually add other columns (D and E), and add 1's / 0's depending on whether or not the fruit were talked about in the document.

I have then created a macro to filter only the documents containing apples based on the 1's and 0's from column D:

Code:

        Sub FilterApple()
            ActiveSheet.Range("B2:F21").AutoFilter Field:=4, Criteria1:="1"
        End Sub

Then, within the code for the userform,

Code:

        Private Sub CommandButton1_Click()
            If CheckBoxApples = True Then Call FilterApples
        End Sub

I created another macro when clicking on the 'Find Documents' button, to call this Filter Apples function if this is true. If it is checked, then the macro works and it sorts by which documents talk about apples.

The problem is I do not know where to go from here (if I want to sort by multiple or even just a different item). I figured there was also probably an easier way to do this as opposed to my method so I figured I would reach out for help. Thanks in advance.
Attached Files

Copy some cell data and write new data based on criteria in a row

$
0
0
New to VBA...of course. i have a spreadsheet that has rows of data that are basically notes. I need to read through those notes and copy some of the other cells in that row to a new sheet as well as write new data to the same new row in the new sheet. Sorry for not stating this well. Basically i have a sheet that has like:

A B C D
row1 10-20-19 randomcelldata1 randomcelldata2 TAXI, XY-exam, CC-inj, new data, CH

I need to copy that first cell with the date (10-20-19) to row 1, cell A a new sheet
Where i see XY-exam i need to write the number 1 in row 1 cell B in the new sheet.
Then where it says CC-inj i need to write number 2 in row 1 cell C in the new sheet.
And where it says CH i need to write CH in row 1 column D in the new sheet.

Then skip to next row and repeat. Thanks!

Is this code to generate subsets of all combinations of list items reasonable?

$
0
0
I need to generate subsets of all combinations of some lists. For example, I need all the 4 item combinations of a list of 12 items (so 12 choose 4). I've found several solutions that generate all possible combinations, but the code below actually seems to do what I want without me having to go cull the sets that I'm not looking for. (I do not know VBA, I'm just looking for solutions to borrow.)

I have two questions, though.

1) This was written in 2000, and the code below includes a 2005 modification to swap Cells.Count out for Cells.CountLarge (see the commented line about a quarter of the way through), and it does seem to work on small test cases, but is there any problem with running this in newer versions of Excel?

2) I'll probably mostly be running this to generate in the range of 20,000 row (21 choose 5), but may need to generate on the order of 175,000 rows at some point (25 choose 6). Is that a bad idea?

Thanks for any insight!


Code:

Option Explicit
Dim vAllItems As Variant
Dim Buffer() As String
Dim BufferPtr As Long
Dim Results As Worksheet
'
'  Posted by Myrna Larson
'  July 25, 2000
'  Microsoft.Public.Excel.Misc
'  Subject:  Combin
'
'
'Since you asked, here it is. It is generic, i.e. it isn't written specifically
'for a given population and set size, as yours it. It will do permutations or
'combinations. It uses a recursive routine to generate the subsets, one routine
'for combinations, a different one for permutations.
'To use it, you put the letter C or P (for combinations or permutations) in a
'cell. The cell below that contains the number of items in a subset. The Cells
'below are a list of the items that make up the population. They could be
'numbers, letters and symbols, or words, etc.
'You select the top cell, or the entire range and run the sub. The subsets are
'written to a new sheet in the workbook.
'
'
Sub ListPermutations()
  Dim Rng As Range
  Dim PopSize As Integer
  Dim SetSize As Integer
  Dim Which As String
  Dim N As Double
  Const BufferSize As Long = 4096
  Set Rng = Selection.Columns(1).Cells
  If Rng.Cells.Count = 1 Then
    Set Rng = Range(Rng, Rng.End(xlDown))
  End If
  PopSize = Rng.Cells.Count - 2
  If PopSize < 2 Then GoTo DataError
  SetSize = Rng.Cells(2).Value
  If SetSize > PopSize Then GoTo DataError
  Which = UCase$(Rng.Cells(1).Value)
  Select Case Which
  Case "C"
    N = Application.WorksheetFunction.Combin(PopSize, SetSize)
  Case "P"
    N = Application.WorksheetFunction.Permut(PopSize, SetSize)
  Case Else
    GoTo DataError
  End Select
'  If N > Cells.Count Then GoTo DataError
  If N > Cells.CountLarge Then GoTo DataError
  Application.ScreenUpdating = False
  Set Results = Worksheets.Add
  vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value
  ReDim Buffer(1 To BufferSize) As String
  BufferPtr = 0
  If Which = "C" Then
    AddCombination PopSize, SetSize
  Else
    AddPermutation PopSize, SetSize
  End If
  vAllItems = 0
  Application.ScreenUpdating = True
  Exit Sub
DataError:
  If N = 0 Then
    Which = "Enter your data in a vertical range of at least 4 cells. " _
      & String$(2, 10) _
      & "Top cell must contain the letter C or P, 2nd cell is the number " _
      & "of items in a subset, the cells below are the values from which " _
      & "the subset is to be chosen."
  Else
    Which = "This requires " & Format$(N, "#,##0") & _
      " cells, more than are available on the worksheet!"
  End If
  MsgBox Which, vbOKOnly, "DATA ERROR"
  Exit Sub
End Sub
Private Sub AddPermutation(Optional PopSize As Integer = 0, _
  Optional SetSize As Integer = 0, _
  Optional NextMember As Integer = 0)
  Static iPopSize As Integer
  Static iSetSize As Integer
  Static SetMembers() As Integer
  Static Used() As Integer
  Dim i As Integer
  If PopSize <> 0 Then
    iPopSize = PopSize
    iSetSize = SetSize
    ReDim SetMembers(1 To iSetSize) As Integer
    ReDim Used(1 To iPopSize) As Integer
    NextMember = 1
  End If
  For i = 1 To iPopSize
    If Used(i) = 0 Then
      SetMembers(NextMember) = i
      If NextMember <> iSetSize Then
        Used(i) = True
        AddPermutation , , NextMember + 1
        Used(i) = False
      Else
        SavePermutation SetMembers()
      End If
    End If
  Next i
  If NextMember = 1 Then
    SavePermutation SetMembers(), True
    Erase SetMembers
    Erase Used
  End If
End Sub  'AddPermutation
Private Sub AddCombination(Optional PopSize As Integer = 0, _
  Optional SetSize As Integer = 0, _
  Optional NextMember As Integer = 0, _
  Optional NextItem As Integer = 0)
  Static iPopSize As Integer
  Static iSetSize As Integer
  Static SetMembers() As Integer
  Dim i As Integer
  If PopSize <> 0 Then
    iPopSize = PopSize
    iSetSize = SetSize
    ReDim SetMembers(1 To iSetSize) As Integer
    NextMember = 1
    NextItem = 1
  End If
  For i = NextItem To iPopSize
    SetMembers(NextMember) = i
    If NextMember <> iSetSize Then
      AddCombination , , NextMember + 1, i + 1
    Else
      SavePermutation SetMembers()
    End If
  Next i
  If NextMember = 1 Then
    SavePermutation SetMembers(), True
    Erase SetMembers
  End If
End Sub  'AddCombination
Private Sub SavePermutation(ItemsChosen() As Integer, _
  Optional FlushBuffer As Boolean = False)
  Dim i As Integer, sValue As String
  Static RowNum As Long, ColNum As Long
  If RowNum = 0 Then RowNum = 1
  If ColNum = 0 Then ColNum = 1
  If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then
    If BufferPtr > 0 Then
      If (RowNum + BufferPtr - 1) > Rows.Count Then
        RowNum = 1
        ColNum = ColNum + 1
        If ColNum > 256 Then Exit Sub
      End If
      Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _
        = Application.WorksheetFunction.Transpose(Buffer())
      RowNum = RowNum + BufferPtr
    End If
    BufferPtr = 0
    If FlushBuffer = True Then
      Erase Buffer
      RowNum = 0
      ColNum = 0
      Exit Sub
    Else
      ReDim Buffer(1 To UBound(Buffer))
    End If
  End If
  'construct the next set
  For i = 1 To UBound(ItemsChosen)
    sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1)
  Next i
  'and save it in the buffer
  BufferPtr = BufferPtr + 1
  Buffer(BufferPtr) = Mid$(sValue, 3)
End Sub  'SavePermutation


Looping VBA code for Goalseeking skipping certain named sheets

$
0
0
Hi!

I tried to do something like this a year ago or so but it never quite came out how originally planned it and now its more important that I get it right.

I have a master sheet with data that references many sheets in the same wb. On each of the many sheets I need one cell to be goalseeked to zero. Right now I have the following few lines of code on each sheet that requires goalseeking:


[Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Range("D4").GoalSeek Goal:=0, ChangingCell:=Range("C13")
Application.EnableEvents = True
End Sub]

This was sufficient for a while but what I need now is a looping code I can activate with a button. One that goes through each sheet in the wb but skips certain named sheets.

or even better...

Is it possible that if I had a list of the names of the certain sheets that I wanted the code to run on that if I selected this list of names and ran the code it would only run on those sheets?

your help is appreciated.

Thanks,

G

Copy a cell to another cell determined by address in a third cell

$
0
0
Hello,
I'm a new vba user so my knowledge is very limited.

I have data in cell F6 (for example, 3.309 but this is a variable) that I want to copy to another cell in the same worksheet. Cell H6, with current content ($G$25) is the cell address where I want to copy the data from cell F6 to. The contents of cell H6 vary based on other conditions that I have set up previously. i.e. Cell H6 might be ($G$25) one time and might be ($D$80) or ($E$30) the next , etc. How do I do this in VBA when executing a macro? In the first instance, the end result should be the number 3.309 copied to cell $G$25.

Thank you.

Why is this happening? Pressing Delete extends range for code?

$
0
0
Hi all, I don't think this is a major functionality issue, but I am very curious as to why this is happening...

I have a simple code that works perfectly:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Range("A:A")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then

Range(Target.Address).Offset(0, 1) = Application.UserName

End If
End Sub

However, by pure chance, I noticed that if I highlight say A1:D5 and hit Delete (only delete does this) it pasts the name in every cell in B2:F5

why on earth does it do that? if i do the same thing but hit any other key it doesn't do that, but Delete does this weird thing?

Any insight is appreciated. Thanks

[SOLVED] Check rows in 2 sheets are the same, else copy rows down

$
0
0
Hi all

Please see attached, I need a piece of code that checks the number or rows in the "Paste" tab, and compares to the "OBS Value" tab
If there are more, then I need to copy down the rows to match.
In the attached example, ther are 14 rows in "Paste" an only 8 in "OBS Value", so 6 rows need to be copied

Thank you in advance
Attached Files

Pivot Table refresh

$
0
0
Hi,

My code just seems to error out on this line of code:

Capture.PNG

The error message I get is

Capture.PNG

Can anyone please help me?

Thanks in advance!
Viewing all 50319 articles
Browse latest View live


Latest Images