Case_when in mutate for weighted averages - creating unexpected NAs for some cases that meet a condition but working for all others - conditional-statements
This is my first post! I have a dataset in long form where i have a column called "indicator" with 51 levels and then a column with numeric values and then a series of columns with identifying and weighting variables. I am using a long sequence of case_when conditions inside mutate to weight different levels of indicator by different other columns. The code is mostly working, but there are a few conditions where it is creating NAs for some of the levels of indicator that meet the condition, but not others. So this means it's not a problem in the line of the code, but something in the underlying data and I can't figure it out. I have tried separating out the distinct conditions into separate mutate functions but that didn't help. All help would be so much appreciated!
# Weighted means # Grouping by indicator only - this is where I get errors
df4 <- df3 %>%
group_by(indicator) %>%
mutate(globalmean = case_when(
((grepl("production", mean_weighting) & grepl("Emissions intensity, beef", indicator)) ~ weighted.mean(value, prod_beef, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, cereals (excl rice)", indicator)) ~ weighted.mean(value, prod_cerealsnorice, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, chicken", indicator)) ~ weighted.mean(value, prod_chickenmeat, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, eggs", indicator)) ~ weighted.mean(value, prod_eggs, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, milk", indicator)) ~ weighted.mean(value, prod_cowmilk, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, pork", indicator)) ~ weighted.mean(value, prod_pork, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, rice", indicator)) ~ weighted.mean(value, prod_rice, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, cereals", indicator)) ~ weighted.mean(value, areaharvested_cereals, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, citrus", indicator)) ~ weighted.mean(value, areaharvested_citrus, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, fruit", indicator)) ~ weighted.mean(value, areaharvested_fruit, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, pulses", indicator)) ~ weighted.mean(value, areaharvested_pulses, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, roots and tubers", indicator)) ~ weighted.mean(value, areaharvested_roottuber, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, treenuts", indicator)) ~ weighted.mean(value, areaharvested_treenuts, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, vegetables", indicator)) ~ weighted.mean(value, areaharvested_vegetables, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, beef", indicator)) ~ weighted.mean(value, producinganimals_beef, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, chicken", indicator)) ~ weighted.mean(value, producinganimals_chickenmeat, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, milk", indicator)) ~ weighted.mean(value, producinganimals_cowmilk, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, eggs", indicator)) ~ weighted.mean(value, producinganimals_eggs, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, pork", indicator)) ~ weighted.mean(value, producinganimals_pork, na.rm = TRUE)),
(grepl("pop_u", mean_weighting) ~ weighted.mean(value, pop_u, na.rm = TRUE)),
(grepl("GDP", mean_weighting) ~ weighted.mean(value, GDP, na.rm = TRUE)), # one of two indicator levels meeting this condition works, the other results in NA
(grepl("unweighted", mean_weighting) ~ mean(value, na.rm = TRUE)),
(grepl("cropland", mean_weighting) ~ weighted.mean(value, croplandcov, na.rm = TRUE)),
(grepl("totalpop", mean_weighting) ~ weighted.mean(value, totalpop, na.rm = TRUE)), # Almost all of the indicator levels meeting this condition work, 3 result in NA
(grepl("landarea", mean_weighting) ~ weighted.mean(value, landarea, na.rm = TRUE)),
TRUE ~ mean(value)
)
) %>%
## Tried this manual overwriting the NA indicator levels as a workaround but it doesn't fix the problem:
mutate(globalmean = case_when(
indicator == "safeh20" ~ weighted.mean(value, totalpop, na.rm = TRUE),
indicator == "govteffect" ~ weighted.mean(value, totalpop, na.rm = TRUE),
indicator == "healthtax" ~ weighted.mean(value, totalpop, na.rm = TRUE),
indicator == "aginGDP" ~ weighted.mean(value, GDP, na.rm = TRUE),
indicator == "functionalintegrity" ~ weighted.mean(value, croplandcov, na.rm = TRUE),
indicator == "pesticides" ~ weighted.mean(value, croplandcov, na.rm = TRUE),
indicator == "sustNO2mgmt" ~ weighted.mean(value, croplandcov, na.rm = TRUE),
TRUE ~ globalmean)) %>%
ungroup()
df4 <- df4 %>% group_by(indicator) %>%
mutate(globalmedian = median(value, na.rm = TRUE),
globalmin = min(value, na.rm = TRUE),
globalmax = max(value, na.rm = TRUE),
globalp25 = quantile(value, c(.25)),
globalp75 = quantile(value, c(.75))) %>%
ungroup() %>% as.data.frame()
**# Weighted means grouping by region and indicator - otherwise all the same code and it works perfectly.**
df5 <- df4 %>% group_by(fsci_regions, indicator) %>%
mutate(regionmean = case_when(
((grepl("production", mean_weighting) & grepl("Emissions intensity, beef", indicator)) ~ weighted.mean(value, prod_beef, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, cereals (excl rice)", indicator)) ~ weighted.mean(value, prod_cerealsnorice, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, chicken", indicator)) ~ weighted.mean(value, prod_chickenmeat, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, eggs", indicator)) ~ weighted.mean(value, prod_eggs, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, milk", indicator)) ~ weighted.mean(value, prod_cowmilk, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, pork", indicator)) ~ weighted.mean(value, prod_pork, na.rm = TRUE)),
((grepl("production", mean_weighting) & grepl("Emissions intensity, rice", indicator)) ~ weighted.mean(value, prod_rice, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, cereals", indicator)) ~ weighted.mean(value, areaharvested_cereals, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, citrus", indicator)) ~ weighted.mean(value, areaharvested_citrus, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, fruit", indicator)) ~ weighted.mean(value, areaharvested_fruit, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, pulses", indicator)) ~ weighted.mean(value, areaharvested_pulses, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, roots and tubers", indicator)) ~ weighted.mean(value, areaharvested_roottuber, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, treenuts", indicator)) ~ weighted.mean(value, areaharvested_treenuts, na.rm = TRUE)),
((grepl("areaharvested", mean_weighting) & grepl("Yield, vegetables", indicator)) ~ weighted.mean(value, areaharvested_vegetables, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, beef", indicator)) ~ weighted.mean(value, producinganimals_beef, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, chicken", indicator)) ~ weighted.mean(value, producinganimals_chickenmeat, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, milk", indicator)) ~ weighted.mean(value, producinganimals_cowmilk, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, eggs", indicator)) ~ weighted.mean(value, producinganimals_eggs, na.rm = TRUE)),
((grepl("producinganimals", mean_weighting) & grepl("Yield, pork", indicator)) ~ weighted.mean(value, producinganimals_pork, na.rm = TRUE)),
((grepl("unweighted", mean_weighting)) ~ mean(value, na.rm = TRUE)),
(grepl("totalpop", mean_weighting) ~ weighted.mean(value, totalpop, na.rm = TRUE)),
(grepl("landarea", mean_weighting) ~ weighted.mean(value, landarea, na.rm = TRUE)),
(grepl("cropland", mean_weighting) ~ weighted.mean(value, croplandcov, na.rm = TRUE)),
(grepl("pop_u", mean_weighting) ~ weighted.mean(value, pop_u, na.rm = TRUE)),
(TRUE ~ mean(value, na.rm = TRUE)))) %>% ungroup() %>%
group_by(fsci_regions, short_label, globalmean, globalmedian, globalmin, globalmax, globalp25,
globalp75, theme, direction) %>%
summarise(regionmean = first(regionmean))
I moved things around in the case_when ordering going from most specific to most general, before that even more lines had errors but now it's down to the 6 described above. I also tried coding more specifically the 6 that aren't working after the main chunk of code but that also didn't work (see annotation in code).
All insights would be so helpful!
Many thanks,
Kate
Related
PHYTON: Problem with loop while in hangman game
When i put a letter in the code it works well but when im gona out another letter the code reboots and the former letter disappear. For example, i put "a" and the word is in fifth place, the code ask me another time, i put another, and the letter "a" dissapear, what can i do :( Help me aadsadas aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa from random import * lista_palabras = ["arbol","flor","melocoton","alicante","deportes","tecnologia","felicidad","emilio"] palabra =choice(lista_palabras) palabra = palabra.upper() respuesta = input("Elige una letra: ") respuesta = respuesta.upper() num_letras = len(palabra) #esto cuenta cuantas letras tiene una palabra intentos = 8 lista = [] lista.append("-" * num_letras) # esto aƱade un guion por cada letra que tenga la palabra lista = " ".join(lista) while intentos > 1: if respuesta in palabra: ind = palabra.index(respuesta) if ind == 0: print(respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind ==1: print(" " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind ==2: print(" " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind == 3: print(" " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind == 4: print(" " + " " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind == 5: print(" " + " " + " " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind == 6: print(" " + " " + " " + " " + " " + " " + respuesta) continue elif ind == 7: print(" " + " " + " " + " " + " " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind == 8: print(" " + " " + " " + " " + " " + " " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue elif ind == 9: print(" " + " " + " " + " " + " " + " " + " " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue else: print(" " + " " + " " + " " + " " + " " + " " + " " + " " + " " + respuesta) print(lista) print(f"{intentos} intentos") respuesta = input("Elige otra letra: ") continue else: intentos = intentos-1 print(lista) print(f"{intentos} intentos") respuesta = input("Elige una letra: ") continue
How do I move the position of the player based on user input. (Represented by O). Code I have written So far. Pls answer in vb
Module Program Dim startx As Integer Dim starty As Integer Dim endx As Integer Dim endy As Integer Dim maze As String(,) = { {"x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x"}, {"O ", " ", " ", " ", " ", " ", " ", " ", " ", "x"}, {"x ", "x ", "x ", "x ", "x ", " ", "x ", "x ", " ", "x"}, {"x ", "x ", " ", "x ", "x ", " ", "x ", "x ", " ", "x"}, {"x ", "x ", " ", " ", " ", " ", "x ", "x ", " ", "x"}, {"x ", "x ", "x ", "x ", "x ", " ", "x ", "x ", "x ", "x"}, {"x ", " ", " ", " ", " ", " ", " ", " ", " ", "x"}, {"x ", " ", "x ", "x ", "x ", "x ", "x ", "x ", " ", "x"}, {"x ", " ", " ", " ", " ", "x ", "x ", "x ", " ", " "}, {"x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x"}} Sub Main(args As String()) Console.WriteLine("This is a maze game") Console.WriteLine("Please enter where you want to go (x and y cordinate)") start displaychessboard() End Sub Sub displaychessboard() Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9") For x As Integer = 0 To 9 Console.Write(x & " ") For y As Integer = 0 To 9 Console.Write(maze(x, y)) Next Console.WriteLine() Next End Sub Sub makemove() End Sub End Module
How do I move the position of the player based on user input. You store the position of a player in one or more variables. Since you have a two-dimensional maze, you can use a two-element tuple or, for simplicity, a player_x and a player_y variable. When the user provides input, you modify the variables accordingly. For example, "moving left" would decrease player_x by one. When drawing your maze, you look at the current values of player_x and player_y. If they match the current position in your loop, you output "O" instead of a space. Pls answer in vb The Stack Overflow community will gladly help you understand concepts and teach you how to improve your Visual Basic skills, but we are not a "here is my task, you write code" service.
VBA: Creating series in a chart
I am trying to create two different series but if I run the macro the second series keeps overwriting the first one unfortunately. So that at the end, I only have the second series in my chart. Can anyone help? With ChtObj 'Series LTU Set Ser = .Chart.SeriesCollection.NewSeries With Ser .Name = "=" & Dataws.Cells(CurrentRow, 1).Address(False, False, xlA1, xlExternal) .XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) .Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal) End With 'Series LTA With Ser .Name = "LTA_" & Dataws.Cells(CurrentRow, 1) .XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 14), Dataws.Cells(CurrentRow, 22)).Address(False, False, xlA1, xlExternal) .Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 42), Dataws.Cells(CurrentRow, 50)).Address(False, False, xlA1, xlExternal) End With End With Thank you!
The problem is you are using the same Ser, so this is replaced. With ChtObj Set Ser = .Chart.SeriesCollection.NewSeries With Ser .Name = "=" & Dataws.Cells(CurrentRow, 1).Address(False, False, xlA1, xlExternal) .XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) .Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal) End With 'Series LTA Set Ser = .Chart.SeriesCollection.NewSeries With Ser .Name = "LTA_" & Dataws.Cells(CurrentRow, 1) .XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 14), Dataws.Cells(CurrentRow, 22)).Address(False, False, xlA1, xlExternal) .Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 42), Dataws.Cells(CurrentRow, 50)).Address(False, False, xlA1, xlExternal) End With End With Try defining a new series again or use a different name for Series2.
How can I make this code run efficiently, currently it takes a very long time for it to run the code
This workbook is used to track projects and I have this VBA code linked to a Form Control button, when I press the button it will run through and get information from all the project sheets and feed it to appropriate areas. I want to figure out how I can combine some of these loops where it reads through my entire work book. Here is my code : Sub Run_ALL_InfoMacros() 'Module 5 = WIG Sheet1, for all information to be on one sheet With Worksheets("Sheet1") ' Clear previous data on the All projects page .Rows("2:" & Rows.Count).ClearContents For Each ws In ThisWorkbook.Worksheets If ws.Range("A5") = "Project # :" Then x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$B$5" 'Project # .Cells(x, "C").Formula = "='" & ws.Name & "'!$A$1" 'Project Name .Cells(x, "D").Formula = "='" & ws.Name & "'!$B$8" 'Project Engineer .Cells(x, "E").Formula = "='" & ws.Name & "'!$B$6" 'Maximo Time Charge .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$5" 'Material Forecast due date .Cells(x, "G").Formula = "=IF('" & ws.Name & "'!$E$11>0,'" & ws.Name & "'!$E$11,TEXT(,))" '.Cells(x, "G").Formula = "='" & ws.Name & "'!$E$11" 'Materials Forecast Actual .Cells(x, "H").Formula = "='" & ws.Name & "'!$F$11" 'Forecast success .Cells(x, "I").Formula = "='" & ws.Name & "'!$F$12" 'IFC Success .Cells(x, "J").Formula = "='" & ws.Name & "'!$E$6" '30% Due '.Cells(x, "K").Formula = "='" & ws.Name & "'!$E$13" '30% actual .Cells(x, "K").Formula = "=IF('" & ws.Name & "'!$E$13>0,'" & ws.Name & "'!$E$13,TEXT(,))" .Cells(x, "L").Formula = "='" & ws.Name & "'!$F$13" '30% success .Cells(x, "M").Formula = "='" & ws.Name & "'!$E$7" '60% due '.Cells(x, "N").Formula = "='" & ws.Name & "'!$E$14" '60% actual .Cells(x, "N").Formula = "=IF('" & ws.Name & "'!$E$14>0,'" & ws.Name & "'!$E$14,TEXT(,))" .Cells(x, "O").Formula = "='" & ws.Name & "'!$F$14" '60% Success .Cells(x, "P").Formula = "='" & ws.Name & "'!$E$8" '90% due '.Cells(x, "Q").Formula = "='" & ws.Name & "'!$E$15" '90% actual .Cells(x, "Q").Formula = "=IF('" & ws.Name & "'!$E$15>0,'" & ws.Name & "'!$E$15,TEXT(,))" .Cells(x, "R").Formula = "='" & ws.Name & "'!$F$15" '90% success .Cells(x, "S").Formula = "='" & ws.Name & "'!$B$11" 'In-service Due '.Cells(x, "T").Formula = "='" & ws.Name & "'!$E$16" 'In-service actual .Cells(x, "T").Formula = "=IF('" & ws.Name & "'!$E$16>0,'" & ws.Name & "'!$E$16,TEXT(,))" .Cells(x, "U").Formula = "='" & ws.Name & "'!$F$16" 'In-service Success .Cells(x, "V").Formula = "='" & ws.Name & "'!$E$4" 'IFC Scheduled '.Cells(x, "W").Formula = "='" & ws.Name & "'!$E$12" 'IFC Actual .Cells(x, "W").Formula = "=IF('" & ws.Name & "'!$E$12>0,'" & ws.Name & "'!$E$12,TEXT(,))" .Cells(x, "X").Formula = "='" & ws.Name & "'!$B$15" 'Non Stores Items .Cells(x, "Y").Formula = "='" & ws.Name & "'!$B$16" 'Non Stores Items Ordered on time .Cells(x, "Z").Formula = "='" & ws.Name & "'!$A$17" 'Non Stores Items Success .Cells(x, "AA").Formula = "='" & ws.Name & "'!$B$17" 'Non Stores Items Percentage End If Next End With 'Module 7 = WIG current & upcoming Projects, for all projects with NO Actual In-service Date Inputted With Worksheets("Current & Upcoming Projects") ' Clear previous data on the All projects page .Rows("3:" & Rows.Count).ClearContents For Each ws In ThisWorkbook.Worksheets If ws.Range("A5") = "Project # :" And ws.Range("E16") = "" Then x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$B$5" 'Project # .Cells(x, "C").Formula = "='" & ws.Name & "'!$A$1" 'Project Name .Cells(x, "D").Formula = "='" & ws.Name & "'!$B$8" 'Project Engineer .Cells(x, "E").Formula = "='" & ws.Name & "'!$B$11" 'In-service Due .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$6" '30% Due .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$13" '30% Success .Cells(x, "H").Formula = "='" & ws.Name & "'!$E$7" '60% due .Cells(x, "I").Formula = "='" & ws.Name & "'!$F$14" '60% Success .Cells(x, "J").Formula = "='" & ws.Name & "'!$E$8" '90% due .Cells(x, "K").Formula = "='" & ws.Name & "'!$F$15" '90% Success .Cells(x, "L").Formula = "='" & ws.Name & "'!$E$5" 'Material Forecast due date .Cells(x, "M").Formula = "='" & ws.Name & "'!$F$11" 'Materials Forecast Success .Cells(x, "N").Formula = "='" & ws.Name & "'!$B$15" 'Non Stores Items .Cells(x, "O").Formula = "='" & ws.Name & "'!$B$16" 'Non Stores Items Ordered on time .Cells(x, "P").Formula = "='" & ws.Name & "'!$A$17" 'Non Stores Items Success End If Next End With 'Module 2 = WIG Completed Project Info , For all the projects that are already in-service. With Worksheets("Completed Project Info") ' Clear previous data on the All projects page .Rows("3:" & Rows.Count).ClearContents For Each ws In ThisWorkbook.Worksheets If ws.Range("A5") = "Project # :" And ws.Range("E16") >= Sheet6.Range("F1") Then x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$B$5" 'Project # .Cells(x, "C").Formula = "='" & ws.Name & "'!$A$1" 'Project Name .Cells(x, "D").Formula = "='" & ws.Name & "'!$B$8" 'Project Engineer .Cells(x, "E").Formula = "='" & ws.Name & "'!$B$11" 'In-service Due .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$16" 'In-service Actual .Cells(x, "G").Formula = "='" & ws.Name & "'!$E$6" '30% Due '.Cells(x, "H").Formula = "='" & ws.Name & "'!$E$13" '30% actual .Cells(x, "H").Formula = "='" & ws.Name & "'!$F$13" '30% Success .Cells(x, "I").Formula = "='" & ws.Name & "'!$E$7" '60% due '.Cells(x, "J").Formula = "='" & ws.Name & "'!$E$14" '60% actual .Cells(x, "J").Formula = "='" & ws.Name & "'!$F$14" '60% Success .Cells(x, "K").Formula = "='" & ws.Name & "'!$E$8" '90% due '.Cells(x, "L").Formula = "='" & ws.Name & "'!$E$15" '90% actual .Cells(x, "L").Formula = "='" & ws.Name & "'!$F$15" '90% Success .Cells(x, "M").Formula = "='" & ws.Name & "'!$E$5" 'Material Forecast due date '.Cells(x, "N").Formula = "='" & ws.Name & "'!$E$11" 'Materials Forecast Actual .Cells(x, "N").Formula = "='" & ws.Name & "'!$F$11" 'Materials Forecast Success .Cells(x, "O").Formula = "='" & ws.Name & "'!$B$15" 'Non Stores Items .Cells(x, "P").Formula = "='" & ws.Name & "'!$B$16" 'Non Stores Items Ordered on time End If Next End With 'For Non-Stores Material With Worksheets("Data Sheet") ' Clear previous data on the All projects page .Rows("141:" & Rows.Count).ClearContents For Each ws In ThisWorkbook.Worksheets If ws.Range("A5") = "Project # :" Then Dim Z As Integer Z = 19 Do While Not ws.Range("A" & Z) = "" And Not IsNull(ws.Range("A" & Z)) x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$A$" & Z 'Non-stores material .Cells(x, "D").Formula = "='" & ws.Name & "'!$C$" & Z 'Lead Time .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$" & Z 'Order By Date .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$" & Z 'Date Ordered .Cells(x, "H").Formula = "='" & ws.Name & "'!$G$" & Z 'Goals Met Z = Z + 1 Loop End If Next End With End Sub
If that is your whole code I'd suggest inserting this right after initializing your sub: screenUpdateState = Application.ScreenUpdating statusBarState = Application.DisplayStatusBar calcState = Application.Calculation eventsState = Application.EnableEvents Application.ScreenUpdating = False Application.DisplayStatusBar = False Application.Calculation = xlCalculationManual Application.EnableEvents = False At the very end of your code (above End Sub) reverse it: Application.ScreenUpdating = screenUpdateState Application.DisplayStatusBar = statusBarState Application.Calculation = calcState Application.EnableEvents = eventsState These settings should give you a nice boost in performance.
This is what I do - at the beginning of your code write Call OnStart At the end Write Call OnEnd Somewhere write the following: Public Sub OnEnd() Application.ScreenUpdating = True Application.EnableEvents = True Application.AskToUpdateLinks = True Application.DisplayAlerts = True Application.Calculation = xlAutomatic Application.StatusBar = False End Sub Public Sub OnStart() Application.ScreenUpdating = False Application.EnableEvents = False Application.AskToUpdateLinks = False Application.DisplayAlerts = False Application.Calculation = xlAutomatic ActiveWindow.View = xlNormalView End Sub
Conversion from string "I" to type 'Boolean' is not valid
I am getting this error when I insert several TextBox commands. Conversion from string "I" to type 'Boolean' is not valid The code for which I am getting this error is: If TextBox1.Text = "I" Then TextBox20.Text = "I" And TextBox32.Text = "I" And TextBox44.Text = "I" If TextBox2.Text = "I" Then TextBox19.Text = "I" And TextBox31.Text = "I" & TextBox43.Text = "I" If TextBox3.Text = "I" Then TextBox17.Text = "I" And TextBox29.Text = "I" & TextBox42.Text = "I" If TextBox4.Text = "I" Then TextBox18.Text = "I" And TextBox28.Text = "I" & TextBox41.Text = "I" If TextBox8.Text = "I" Then TextBox16.Text = "I" And TextBox27.Text = "I" & TextBox40.Text = "I" If TextBox7.Text = "I" Then TextBox15.Text = "I" And TextBox26.Text = "I" & TextBox39.Text = "I" If TextBox6.Text = "I" Then TextBox14.Text = "I" And TextBox25.Text = "I" & TextBox38.Text = "I" If TextBox5.Text = "I" Then TextBox13.Text = "I" And TextBox24.Text = "I" & TextBox37.Text = "I" If TextBox30.Text = "I" Then TextBox11.Text = "I" And TextBox23.Text = "I" If TextBox33.Text = "I" Then TextBox10.Text = "I" And TextBox22.Text = "I" & TextBox35.Text = "I" If TextBox48.Text = "I" Then TextBox47.Text = "I" And TextBox21.Text = "I" & TextBox34.Text = "I" If TextBox52.Text = "I" Then TextBox51.Text = "I" And TextBox46.Text = "I" & TextBox45.Text = "I" If TextBox9.Text = "I" Then TextBox12.Text = "I" And TextBox50.Text = "I" & TextBox49.Text = "I" Any suggestions? They all begin with If TextBox not just TextBox
I fix just one line as an example for the other lines. If TextBox1.Text = "I" Then TextBox20.Text = "I" TextBox32.Text = "I" TextBox44.Text = "I" End If The AND operator is used to execute a logical operation between two expression that result in a boolean TRUE/FALSE result not to repeat the execution of a command.