VBA将两个循环组合到一个消息框中

时间:2017-11-04 21:05:35

标签: excel vba excel-vba

以下是提示:查看列表中有多少Zachary(使用您喜欢的任何循环),同时查找列表中有多少客户

我能够制作两个单独的循环,但当我尝试将它们组合时,它们不会在一个消息框中工作。

'查找列表中的Zachary数量(使用您喜欢的任何循环)

counter = 0
zCounter = 0

With Workbooks("C14-Do While Test - final.xlsm").Worksheets("Customers")

    Do While counter < 500
        counter = counter + 1

        If .Range("B1:B500").Cells(counter) = "Zachary" Then
            zCounter = zCounter + 1
        End If
    Loop
    MsgBox "There are " & zCounter & " Zacharys in the list"

End With

&#39;并同时查找列表中有多少客户

counter = 0
notFound = True

With Workbooks("C14-Do While Test - final.xlsm").Worksheets("Customers")

    Do While notFound
        counter = counter + 1

        If .Range("B1:B500").Cells(counter) = "" Then
            notFound = False
        End If
    Loop
    MsgBox "There are " & counter & " customers in the list"

End With

1 个答案:

答案 0 :(得分:1)

尝试以下只有一个循环,并引入一个变量 counter = 0 zCounter = 0 allCustomers = 0 ' variable to hold all customers With Workbooks("C14-Do While Test - final.xlsm").Worksheets("Customers") Do While counter < 500 counter = counter + 1 If .Range("B1:B500").Cells(counter) = "Zachary" Then zCounter = zCounter + 1 End If If .Range("B1:B500").Cells(counter) <> vbNullString Then allCustomers = allCustomers + 1 End If Loop MsgBox "There are " & zCounter & " Zacharys in the list" & vbNewLine & "There are " & allCustomers & " customers in the list" End With 来保存在循环范围内被假定为非空单元格的客户数。

VBNewline与Msgbox一起用于在不同的行中打印两个计数。

.Range("B1:B500").Cells(counter)

您可以将此import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import com.meddis.util.serializer.CustomIgnorePropertyFilter; import com.meddis.util.serializer.CustomSerializerModifier; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.mock.http.MockHttpOutputMessage; import org.springframework.security.authentication.encoding.Md5PasswordEncoder; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; import java.io.IOException; import java.nio.charset.Charset; import static org.junit.Assert.assertNotNull; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; @RunWith(SpringRunner.class) @ActiveProfiles({"test"}) @TestPropertySource(properties = { "timezone = UTC" }) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public abstract class BasicRestTest { protected String host = "localhost"; @Value("${local.server.port}") protected int port; @Value("${spring.data.rest.basePath}") protected String springDataRestBasePath; protected MediaType contentType = new MediaType("application", "hal+json", Charset.forName("utf8")); protected MockMvc mockMvc; private static HttpMessageConverter mappingJackson2HttpMessageConverter; protected ObjectMapper objectMapper; @Autowired private WebApplicationContext webApplicationContext; @Autowired void setConverters(HttpMessageConverter<?>[] converters) { this.objectMapper = new ObjectMapper(); if (this.mappingJackson2HttpMessageConverter == null) { this.mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); SimpleModule simpleModule = new SimpleModule("CUSTOM", Version.unknownVersion()); simpleModule.setSerializerModifier(new CustomSerializerModifier(springDataRestBasePath)); ((MappingJackson2HttpMessageConverter) this.mappingJackson2HttpMessageConverter).getObjectMapper() .registerModule(simpleModule); FilterProvider fp = new SimpleFilterProvider().addFilter("CUSTOM", new CustomIgnorePropertyFilter()); ((MappingJackson2HttpMessageConverter) this.mappingJackson2HttpMessageConverter).getObjectMapper() .setFilterProvider(fp); ((MappingJackson2HttpMessageConverter) this.mappingJackson2HttpMessageConverter).setPrettyPrint(true); } assertNotNull("the JSON message converter must not be null", this.mappingJackson2HttpMessageConverter); } @Before public void setup() throws Exception { this.mockMvc = webAppContextSetup(webApplicationContext).build(); } protected String json(final Object o) throws IOException { MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage(); this.mappingJackson2HttpMessageConverter.write(o, MediaTypes.HAL_JSON, mockHttpOutputMessage); return mockHttpOutputMessage.getBodyAsString(); } } 拉出到变量中以加快循环速度。